简单的 C++程序帮助编译

发布于 2024-10-16 04:45:24 字数 1580 浏览 2 评论 0原文

伙计们,我是一名初级程序员,需要帮助。我正在处理位于此处的作业的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深海蓝天 2024-10-23 04:45:24

我认为你的 makefile 应该看起来更像这样:

all: lab2

lab2: main.o tenstrings.o
    g++ main.o tenstrings.o -o lab2

main.o: main.cpp
    g++ -Wall -c main.cpp -o main.o

tenstrings.o: tenstrings.cpp
    g++ -Wall -c tenstrings.cpp -o tenstrings.o

这可能会解决你的前两个错误。

I think your makefile should look more like this:

all: lab2

lab2: main.o tenstrings.o
    g++ main.o tenstrings.o -o lab2

main.o: main.cpp
    g++ -Wall -c main.cpp -o main.o

tenstrings.o: tenstrings.cpp
    g++ -Wall -c tenstrings.cpp -o tenstrings.o

that might sort out your first two errors.

神也荒唐 2024-10-23 04:45:24

类声明末尾需要有一个分号。另外,您可以更简单地重写您的 Makefile;您不需要每个文件都有一个目标:

all: lab2

lab2: lab2.o
    g++ -Wall lab2.o -o lab2

lab2.o: main.cpp tenstrings.cpp
    g++ -Wall -o lab2.o tenstrings.cpp main.cpp

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:

all: lab2

lab2: lab2.o
    g++ -Wall lab2.o -o lab2

lab2.o: main.cpp tenstrings.cpp
    g++ -Wall -o lab2.o tenstrings.cpp main.cpp
与酒说心事 2024-10-23 04:45:24

您不能使用“;”来终止 TenStrings 类在 TenStrings.h 的末尾,这就是错误所在。

You don't terminate the TenStrings class with a ';' at the end in TenStrings.h, which is what the error is.

野の 2024-10-23 04:45:24

您在类声明末尾缺少一个分号。

You're missing a semi-colon at the end of the class declaration.

乱世争霸 2024-10-23 04:45:24

我认为您忘记了类定义末尾的分号。

/*tenstrings.h*/
#ifndef TENSTRNGS
#define TENSTRNGS

class TenStrings
{
public: 
    // Default Constructor
    TenStrings();
}; // <--- !!!!
#endif

I think you forgot a semicolon at the end of your class definition.

/*tenstrings.h*/
#ifndef TENSTRNGS
#define TENSTRNGS

class TenStrings
{
public: 
    // Default Constructor
    TenStrings();
}; // <--- !!!!
#endif
诠释孤独 2024-10-23 04:45:24

您定义了两次相同的 make 目标。那么你就不会链接项目的所有部分。这应该是这样的:

all: lab2

lab2: lab.o lab2.o
    g++ -Wall lab.o lab2.o -o lab2

lab.o: main.cpp
    g++ -Wall -c main.cpp -o lab.o

lab2.o: tenstrings.cpp
    g++ -Wall -c tenstrings.cpp -o lab2.o

You have the same make target defined twice. Then you don't link all parts of the project. This should be something like:

all: lab2

lab2: lab.o lab2.o
    g++ -Wall lab.o lab2.o -o lab2

lab.o: main.cpp
    g++ -Wall -c main.cpp -o lab.o

lab2.o: tenstrings.cpp
    g++ -Wall -c tenstrings.cpp -o lab2.o
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文