0

I added a new source folder to my code and create a new class with below header and cpp file

#ifndef ENVIRONMENT_H_
#define ENVIRONMENT_H_

#include <string.h>
using namespace std;

namespace daemonWorld {

class Environment {
    const string objName;
public:
    Environment(const string & name){
        this->objName = name;

    }
    virtual ~Environment();
};

} /* namespace daemonWorld */

#endif /* TEMP_ENVIRONMENT_H_ */

CPP file

#include "Environment.h"


namespace daemonWorld {


Environment::~Environment() {
    // TODO Auto-generated destructor stub
}

} /* namespace daemonWorld */

I am getting an error that string is not a type in constructor and the member variable Obj and I am getting Codan error in cpp file Member declaration not found for constructor. I have many times cleaned the project, rebuild the index and rebuild the project but it doesn't work. Any Idea?

Govan
  • 2,079
  • 4
  • 31
  • 53

1 Answers1

3
#include <string.h>

should be

#include <string>

string.h is the C string header. string is the C++ string header.

Furthermore, all standard C++ headers omit the .h. Even the C headers, when included from C++ code should be prefixed with c in addition to omitting the .h. E.g. cstring would be the correct header to include to get the C string header in C++.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • I accepted your answer however I defined the in other files without problem. I think eclipse cdt had a problem with refreshing the indexes some how. After I add a new source folder. It has happened before as well. – Govan Oct 21 '15 at 22:56
  • 3
    @Govan Yes your compiler might allow including `some_std_header.h` but you can't rely on that behavior. As far as the Standard is concerned, those headers don't exist. Relying on implementation-defined behavior is generally bad. – Emil Laine Oct 21 '15 at 23:00