Hi there

I'm trying to learn how to do classes in C++ while working on a project. I have some code (below) which fails to compile with the error (below)

Any help would be appreciated.

Many thanks

packageType.cc: In static member function ‘static packageType* packageType::fromString(std::string)’:
packageType.cc:20: error: request for member ‘toString’ in ‘packageType::NOT_IMPORTANT’, which is of non-class type ‘const packageType* ()()’
packageType.cc:20: error: cannot convert ‘const packageType* (*)()’ to ‘packageType*’ in return
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>

using namespace std;

class packageType{
	private:
	packageType(string name){}
	~packageType(){} 
	string name;
	
	public:
	const static packageType* NOT_IMPORTANT(){return new packageType("Not Important");};
	const static packageType* IMPORTANT(){return new packageType("Important");};
	const static packageType* TESTING(){return new packageType("Testing");};
	string toString();
	static packageType* fromString(string type){
		if(NOT_IMPORTANT->toString().compare(type) == 0){return NOT_IMPORTANT;}
	}
	
};

string packageType::toString(){
	return name;
}

int main(void){
	return(0);
}

Your code isn't const correct and you need to remember that NOT_IMPORTANT is a function, not an object. This compiles, but it may not be what you were looking for:

class packageType{
private:
  packageType(string name){}
  ~packageType(){} 
  string name;

public:
  const static packageType* NOT_IMPORTANT(){return new packageType("Not Important");};
  const static packageType* IMPORTANT(){return new packageType("Important");};
  const static packageType* TESTING(){return new packageType("Testing");};
  string toString() const;
  static const packageType* fromString(string type){
    if(NOT_IMPORTANT()->toString().compare(type) == 0){
      return NOT_IMPORTANT();
    }
  }
};

That looks great, thank you very much for your help. It's tricky stuff this!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.