linux 下 C++ 多个源文件 编译问题
编译器gccg++都装过了,(ubuntu8.03这个好像是没有说的必要),编译C++错误我无法忍受,一直没有解决方案。我仅仅是想测试一下,都不断的报错,我要崩溃了。//...
编译器gcc g++都装过了,(ubuntu8.03这个好像是没有说的必要),编译C++错误我无法忍受,一直没有解决方案。我仅仅是想测试一下,都不断的报错,我要崩溃了。
// try.h
1 #ifndef TRY_H
2 #define TRY_H
3 #include <iostream>
4
5 class try{
6 private:
7 int a;
8 int b;
9 public:
10 try();
11 ~try();
12 void print();
13 };
14 #endif
//try.cpp
1 #include"try.h"
2 try::try()
3 {
4 }
5 try::~try()
6 {
7 }
8 void try::print()
9 {
10 std::cout<<"Class try is this "<<std::endl;
11 }
//main.cpp
1 //#include<iostream>
2 #include<string>
3 #include"try.h"
4
5 using namespace std;
6
7 int main(int argc, char *argv[])
8 {
9 class try x;
10 x.print();
11 std::cout<<"hello world"<<std::endl;
12 return 0;
13 }
konsole: gcc -c main.cpp try.cpp
信息:In file included from main.cpp:3:
try.h:5: error: expected identifier before ‘try’
try.h:5: error: expected unqualified-id before ‘try’
main.cpp: In function ‘int main(int, char**)’:
main.cpp:9: error: expected unqualified-id before ‘try’
main.cpp:10: error: ‘x’ was not declared in this scope
In file included from try.cpp:1:
try.h:5: error: expected identifier before ‘try’
try.h:5: error: expected unqualified-id before ‘try’
try.cpp:2: error: expected unqualified-id before ‘try’
try.cpp:5: error: expected unqualified-id before ‘try’
try.cpp:8: error: expected unqualified-id before ‘try’
哪位牛人指点一二!在此谢过。 展开
// try.h
1 #ifndef TRY_H
2 #define TRY_H
3 #include <iostream>
4
5 class try{
6 private:
7 int a;
8 int b;
9 public:
10 try();
11 ~try();
12 void print();
13 };
14 #endif
//try.cpp
1 #include"try.h"
2 try::try()
3 {
4 }
5 try::~try()
6 {
7 }
8 void try::print()
9 {
10 std::cout<<"Class try is this "<<std::endl;
11 }
//main.cpp
1 //#include<iostream>
2 #include<string>
3 #include"try.h"
4
5 using namespace std;
6
7 int main(int argc, char *argv[])
8 {
9 class try x;
10 x.print();
11 std::cout<<"hello world"<<std::endl;
12 return 0;
13 }
konsole: gcc -c main.cpp try.cpp
信息:In file included from main.cpp:3:
try.h:5: error: expected identifier before ‘try’
try.h:5: error: expected unqualified-id before ‘try’
main.cpp: In function ‘int main(int, char**)’:
main.cpp:9: error: expected unqualified-id before ‘try’
main.cpp:10: error: ‘x’ was not declared in this scope
In file included from try.cpp:1:
try.h:5: error: expected identifier before ‘try’
try.h:5: error: expected unqualified-id before ‘try’
try.cpp:2: error: expected unqualified-id before ‘try’
try.cpp:5: error: expected unqualified-id before ‘try’
try.cpp:8: error: expected unqualified-id before ‘try’
哪位牛人指点一二!在此谢过。 展开
3个回答
展开全部
-c 只编译不连接
应该是这样:
g++ file1 -c -o file1.o
g++ file2 -c -o file2.o
g++ file1.o file.o -o exec
应该是这样:
g++ file1 -c -o file1.o
g++ file2 -c -o file2.o
g++ file1.o file.o -o exec
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.try是 关键字 不能用作 class 名字,要换个名字
2.第9行定义类的对象,前面不必加 class
2.第9行定义类的对象,前面不必加 class
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询