简单的 C++程序帮助编译
伙计们,我是一名初级程序员,需要帮助。我正在处理位于此处的作业的 A 部分:http:// /cs.binghamton.edu/~sgreene/cs240-2010f/labs/lab2.html
但是我在编译程序时遇到了问题。请告诉我我的 makefile 和 cpp 文件中做错了什么!这是我的文件:
# CS240 Lab2 Makefile
all: lab2
lab2: lab2.o
g++ -Wall lab2.o -o lab2
lab2.o: main.cpp
g++ -Wall -c main.cpp -o lab2.o
lab2.o: tenstrings.cpp
g++ -Wall -c tenstrings.cpp -o lab2.o
/*tenstrings.h*/
#ifndef TENSTRNGS
#define TENSTRNGS
class TenStrings
{
public:
// Default Constructor
TenStrings();
}
#endif
/* tenstrings.cpp */
#include "TenStrings.h"
//Default Constructor
TenStrings::TenStrings()
{}
/* main.cpp */
#include "TenStrings.h"
int main()
{
TenStrings varTen;
return 0;
}
正如您所看到的,这甚至不是一个程序,它只是让我开始编译,但我收到了一堆错误:
________________________________________________________________________________________
Makefile:12: warning: overriding commands for target 'lab2.o'
Makefile:9: warning: ignoring old commands for target 'lab2.o'
g++ -Wall -c tenstrings.cpp -o lab2.o
In file included from tenstrings.cpp:6:
TenStrings.h:11:3: warning: no newline at end of file
tenstrings.cpp:7: error: new types may not be defined in a return type
tenstrings.cpp:7: error: return type specification for constructor invalid
_________________________________________________________________________________________
提前致谢!
Guys I am a starting programmer and need help. I am working on PART A ONLY of an assignment located here: http://cs.binghamton.edu/~sgreene/cs240-2010f/labs/lab2.html
However I am having trouble compiling my programs. Please tell me what im doing wrong in my makefile and in the cpp files! Here are my files:
# CS240 Lab2 Makefile
all: lab2
lab2: lab2.o
g++ -Wall lab2.o -o lab2
lab2.o: main.cpp
g++ -Wall -c main.cpp -o lab2.o
lab2.o: tenstrings.cpp
g++ -Wall -c tenstrings.cpp -o lab2.o
/*tenstrings.h*/
#ifndef TENSTRNGS
#define TENSTRNGS
class TenStrings
{
public:
// Default Constructor
TenStrings();
}
#endif
/* tenstrings.cpp */
#include "TenStrings.h"
//Default Constructor
TenStrings::TenStrings()
{}
/* main.cpp */
#include "TenStrings.h"
int main()
{
TenStrings varTen;
return 0;
}
As you can see this isnt even a program, its just to get me started in compiling but i am getting bunch of errors:
________________________________________________________________________________________
Makefile:12: warning: overriding commands for target 'lab2.o'
Makefile:9: warning: ignoring old commands for target 'lab2.o'
g++ -Wall -c tenstrings.cpp -o lab2.o
In file included from tenstrings.cpp:6:
TenStrings.h:11:3: warning: no newline at end of file
tenstrings.cpp:7: error: new types may not be defined in a return type
tenstrings.cpp:7: error: return type specification for constructor invalid
_________________________________________________________________________________________
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为你的 makefile 应该看起来更像这样:
这可能会解决你的前两个错误。
I think your makefile should look more like this:
that might sort out your first two errors.
类声明末尾需要有一个分号。另外,您可以更简单地重写您的 Makefile;您不需要每个文件都有一个目标:
You need a semicolon at the end of the class declaration. Also, you can rewrite your Makefile a bit more simply; you don't need a target for each file:
您不能使用“;”来终止 TenStrings 类在 TenStrings.h 的末尾,这就是错误所在。
You don't terminate the TenStrings class with a ';' at the end in TenStrings.h, which is what the error is.
您在类声明末尾缺少一个分号。
You're missing a semi-colon at the end of the class declaration.
我认为您忘记了类定义末尾的分号。
I think you forgot a semicolon at the end of your class definition.
您定义了两次相同的
make
目标。那么你就不会链接项目的所有部分。这应该是这样的:You have the same
make
target defined twice. Then you don't link all parts of the project. This should be something like: