gpt4 book ai didi

C++代码无法编译

转载 作者:行者123 更新时间:2023-11-28 00:18:11 26 4
gpt4 key购买 nike

<分区>

你好,我正在尝试使用 g++ 在 Ubuntu 中编译我的 c++ 代码。

我有 3 个文件:Ball.h Ball.cpp main.cpp

头文件Ball.h

#ifdef BALL_H_
#define BALL_H_
class Ball {

public:
Ball();
Ball(int radius);
~Ball();

void setRadius(int);
int getRadius();

private:

int radius;


};

#endif //BALL_H_

球.cpp文件

#include "Ball.h"

Ball::Ball() {

radius = 1;

}

Ball::Ball(int rad) {

radius = rad;

}

Ball::~Ball() {

}

void Ball::setRadius(int rad) {

radius = rad;

}

int Ball::getRadius() {

return radius;

}

main.cpp 文件

#include <iostream>
#include "Ball.h"


using namespace std;

int main() {

//Ball b, b2, b3(2);

Ball b = Ball();
Ball b2 = Ball();
Ball b3 = Ball(2);

cout << "Initial radius: ";
<<b.getRadius() << endl;

b.setRadius(10);

cout << "New radius: "
<< b.getRadius() << endl;

cout << "Initial radius b2: "
<< b2.getRadius() << endl;

b2.setRadius(20);

cout << "b/b2"
<< b.getRadius() << "/"
<< b2.getRadius() << endl;


cout <<"b3 radius: "
<< b3.getRadius() << endl;
return 0;

}

当我试图编译所有这 3 个文件时,编译器甚至找不到在 main.cpp 中声明的 Ball 对象我正在使用 makefile 编译这 3 个文件。

生成文件:

GPP=g++
GPPOPT=-std=c++11

OBJ=Ball main

all: $(OBJ)

Ball:
$(GPP) $(GPPOPT) -c Ball.cpp -o $@

main:
$(GPP) $(GPPOPT) main.cpp -o $@


clean:
-$(RM) $(OBJ)

编译器消息:

m4r1us@ubuntu:~/Desktop/cpptest$ make
g++ -std=c++11 -c Ball.cpp -o Ball
Ball.cpp:3:2: error: ‘Ball’ does not name a type
Ball::Ball() {
^
Ball.cpp:9:2: error: ‘Ball’ does not name a type
Ball::Ball(int rad) {
^
Ball.cpp:15:2: error: ‘Ball’ does not name a type
Ball::~Ball() {
^
Ball.cpp:19:7: error: ‘Ball’ has not been declared
void Ball::setRadius(int rad) {
^
Ball.cpp: In function ‘void setRadius(int)’:
Ball.cpp:21:2: error: ‘radius’ was not declared in this scope
radius = rad;
^
Ball.cpp: At global scope:
Ball.cpp:25:6: error: ‘Ball’ has not been declared
int Ball::getRadius() {
^
Ball.cpp: In function ‘int getRadius()’:
Ball.cpp:27:9: error: ‘radius’ was not declared in this scope
return radius;
^
make: *** [Ball] Error 1
m4r1us@ubuntu:~/Desktop/cpptest$

我认为编译有一些问题。我不知道我做错了什么。对我有什么建议吗?

谢谢。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com