我有一个 my.h 文件:
#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
}
我想创建一个so文件。创建的时候没有错误。但是当我编译 .h 文件时,它显示: string:No such file or directory。如果我使用 pus string.h 而不是 string,则会出现错误:预期 '=',',',';','asm', before S (at class S) in my.h 。 在 .cpp 文件中(如果我用 string.h 更改字符串),编译后出现错误:命名空间 std 中的字符串未命名类型。我哪里错了?
最佳答案
好吧,首先,你似乎来自 java,因为当你输入:
class S
{
public: S();
std::string myname;
};
我猜你实际上的意思是:
class S
{
public:
S();
private:
std::string myname;
};
在 .cpp
文件中,您键入了 s
而不是 S
:请注意,C++ 对于类名称而言区分大小写。
此外,关于您的问题,我怀疑您当前使用的是 C
编译器,而不是 C++
编译器。在不知道所使用的命令行的情况下,我无法对此说太多。
关于c++ - linux ubuntu c++ .h 和 .cpp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5815823/