开发者

linux ubuntu c++ .h and .cpp file

I have a my.h file:

#ifndef __MY__
#define __MY__

#include <string>
#include <time.h>

class S
{
    public: S();
    std::string myname;
};

#endif

my.cpp

#include "my.h";

#include<string>
#include<iostream>

using namespace std;

S::S()
{
    // .. 开发者_开发技巧code
}

I want to create an so file. There is no error when creating it. But when I compile the .h file it says: string:No such file or directory. If I pus string.h instead of string I have the error: expected '=',',',';','asm', before S (at class S) in my.h. In the .cpp file (if i change the string with string.h) i have after i compile error: string in namespace std does not name a type. WHERE AM I WRONG?


Well, first, it seems that you come from java because when you typed:

class S
{
  public: S();
  std::string myname;
};

I guess you actually meant:

class S
{
  public:

    S();

  private:

    std::string myname;
};

In the .cpp file, you typed s instead of S: note that C++ is case-sensitive regarding classes names.

Also, regarding your problem, I suspect you are currently using a C compiler and not a C++ compiler. Without knowing the used command-line, I can't say much more on that.


Try this

#ifndef MY_H
#define MY_H
#include <string>
#include <time.h>

class S
{
public: S();
std::string myname;
};

#endif



#include "my.h"
#include<string>
#include<iostream>

using namespace std;

S::S()
{
    //code
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜