FreeOZ论坛

标题: 学习中遇到问题了,哪位老师来给点拨一下吧 [打印本页]

作者: 四香油饼    时间: 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

我的代码如下:
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
作者: coredump    时间: 25-11-2009 12:02
class 声明,最后要有个分号

class XXX {
。。。
};
作者: 四香油饼    时间: 25-11-2009 13:20
原帖由 coredump 于 25-11-2009 12:02 发表
class 声明,最后要有个分号

class XXX {
。。。
};


竟然如此简单!
加上个分号,两种方法都能通过了!(程序还有别的小毛病,那些都好找了)

这个事情说明:我程序写得太少了
作者: yuba    时间: 25-11-2009 13:31
原帖由 四香油饼 于 25-11-2009 13:20 发表
这个事情说明:我程序写得太少了


这个事情说明:编译器太不友好了
作者: 一半    时间: 25-11-2009 15:46
饼叔真是刻苦!
作者: MillerYang    时间: 25-11-2009 15:59
主席辛苦了。。。




欢迎光临 FreeOZ论坛 (https://www.freeoz.org/bbs/) Powered by Discuz! X3.2