标题: 学习中遇到问题了,哪位老师来给点拨一下吧 [打印本页] 作者: 四香油饼 时间: 25-11-2009 11:57 标题: 学习中遇到问题了,哪位老师来给点拨一下吧 在学习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
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作者: coredump 时间: 25-11-2009 12:02
class 声明,最后要有个分号