2011-04-28 106 views
0

我有一个my.h文件:Linux操作系统Ubuntu C++的.h和.cpp文件

#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 
} 

我想创建一个这样的文件。创建它时没有错误。但是,当我编译.h文件它说:字符串:没有这样的文件或目录。如果我pus string.h而不是字符串,我在my.h中的S(在类S)之前有错误:expected'=',',',';','asm'。 在.cpp文件(如果我改变字符串string.h)我有我编译后错误:名称空间中的字符串std不会命名一个类型。我错在哪里?

+1

你的编译命令行是什么?顺便说一句,'__MY__'这样的名称不允许以用户编写的代码创建。 – 2011-04-28 08:37:30

+1

此外,它会更好地删除分号在my.cpp的第一行 – beduin 2011-04-28 08:38:49

+1

...你正在编译.h? – 2011-04-28 08:42:19

回答

0

嗯,首先,你好像来自Java的,因为当你键入:

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

我想你实际上意味着:

class S 
{ 
    public: 

    S(); 

    private: 

    std::string myname; 
}; 

.cpp文件,您键入s而不是S:请注意,C++是case - 对类名称敏感。

另外,关于你的问题,我怀疑你目前使用的编译器是C而不是C++编译器。在不知道使用的命令行的情况下,我不能多说这些。

+0

Thx用户答案。为什么我在cpp中收到错误:命名空间std中的my.h字符串没有命名类型? – linuxx 2011-04-28 08:58:15

+0

我正在Geany计划中工作。我刚刚从菜单中创建。 – linuxx 2011-04-28 08:59:02

+0

你的编译命令行是什么?我不知道Geany是什么。 – ereOn 2011-04-28 09:04:20

0

试试这个

#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

我有.h文件错误:字符串没有这样的文件或目录 – linuxx 2011-04-28 09:08:29