马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册
x
在学习c++基础知识,做习题:),希望能早日写出3000行的代码,可是上来就遇到问题搞不懂了,还请大家给我指点一下:
题目:Create a Text class that contains a string object to hold
the text of a file. Give it two constructors: a default
constructor and a constructor that takes a string
argument that is the name of the file to open. When the
second constructor is used, open the file and read the
contents into the string member object. Add a member
function contents( ) to return the string so (for example)
it can be printed. In main( ), open a file using Text and
print the contents
我的代码如下:
text.h:
#ifndef TEXT_H
#define TEXT_H
#include <string>
class Text
{
public:
std::string Contents(void);
Text();
Text(char *st);
~Text();
private:
std::string File_Content;
}
#endif
text.cpp:
#include "text.h"
#include <iostream>
#include <fstream>
using namespace std;
Text::Text()
{}
Text::Text(char *st)
{
string line;
ifstream In_File(*st);
while (getline(In_File,line))
{
File_Content = File_Content + line + endl;
}
}
Text::~Text()
{}
string Text::Contents(void)
{
return (File_Content);
}
e1.cpp:
#include "text.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
Text file1("text.h");
cout << file1.Contents() << endl;
}
我现在遇到的问题是,一编译,就出现一些奇怪的错误:
g++ -c -o e1.o e1.cpp
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
c++/3.4.5/bits/localefwd.h:48,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
c++/3.4.5/ios:48,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
c++/3.4.5/ostream:45,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
c++/3.4.5/iostream:45,
from e1.cpp:2:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/functex
cept.h:36: error: expected unqualified-id before "namespace"
e1.cpp:10: error: expected declaration before '}' token
make: *** [e1.o] Error 1
我大概知道是namespace出了问题,可是搞不太明白是哪里出了问题,我改了一下text.h,改成下面的样子,
// text.h
#ifndef TEXT_H
#define TEXT_H
#include <string>
class Text
{
using namespace std;
public:
string Contents(void);
Text();
Text(char *st);
~Text();
private:
string File_Content;
}
#endif
错误信息变成了这样,我实在是晕了,老师给指点一下吧
g++ -c -o e1.o e1.cpp
In file included from e1.cpp:1:
text.h:7: error: expected nested-name-specifier before "namespace"
text.h:7: error: expected unqualified-id before "namespace"
text.h:7: error: expected `;' before "namespace"
text.h:7: error: expected unqualified-id before "namespace"
text.h:9: error: `string' does not name a type
text.h:14: error: `string' does not name a type
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
c++/3.4.5/bits/localefwd.h:48,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
c++/3.4.5/ios:48,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
c++/3.4.5/ostream:45,
from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
c++/3.4.5/iostream:45,
from e1.cpp:2:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/functex
cept.h:36: error: expected unqualified-id before "namespace"
e1.cpp: In function `int main(int, char**)':
e1.cpp:8: error: 'class Text' has no member named 'Contents'
e1.cpp: At global scope:
e1.cpp:10: error: expected declaration before '}' token |