找回密码
 FreeOZ用户注册
查看: 3360|回复: 5
打印 上一主题 下一主题

[论坛技术] 学习中遇到问题了,哪位老师来给点拨一下吧

[复制链接]
跳转到指定楼层
1#
发表于 25-11-2009 11:57:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?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
回复  

使用道具 举报

2#
发表于 25-11-2009 12:02:35 | 只看该作者
class 声明,最后要有个分号

class XXX {
。。。
};

评分

参与人数 1威望 +49 收起 理由
四香油饼 + 49 你太厉害了

查看全部评分

回复  

使用道具 举报

3#
 楼主| 发表于 25-11-2009 13:20:39 | 只看该作者


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

这个事情说明:我程序写得太少了
回复  

使用道具 举报

4#
发表于 25-11-2009 13:31:04 | 只看该作者
原帖由 四香油饼 于 25-11-2009 13:20 发表
这个事情说明:我程序写得太少了


这个事情说明:编译器太不友好了

评分

参与人数 1威望 +30 收起 理由
coredump + 30 我很赞同!

查看全部评分

回复  

使用道具 举报

5#
发表于 25-11-2009 15:46:00 | 只看该作者
饼叔真是刻苦!
回复  

使用道具 举报

6#
发表于 25-11-2009 15:59:18 | 只看该作者
主席辛苦了。。。
回复  

使用道具 举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+11, 5-3-2025 02:23 , Processed in 0.030305 second(s), 25 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表