C++ 文件夹(Makefile)中的函数差异问题
由于未声明不同的函数/库,我的终端中有一条消息。我使用makefile,但它可能是一个错误,因为标题,但我无法找到。以下是代码+终端的简历:C++ 文件夹(Makefile)中的函数差异问题,c++,makefile,header,definition,C++,Makefile,Header,Definition,由于未声明不同的函数/库,我的终端中有一条消息。我使用makefile,但它可能是一个错误,因为标题,但我无法找到。以下是代码+终端的简历: my.name@jupyterhub:~/Project$ make test g++ -std=c++11 -Wall -c place.cpp In file included from place.cpp:1:ants.hpp:6:17: error: expected ')' before 'a' 6 | Ants(Coord a
my.name@jupyterhub:~/Project$ make test
g++ -std=c++11 -Wall -c place.cpp
In file included from place.cpp:1:ants.hpp:6:17: error: expected ')' before 'a'
6 | Ants(Coord a, int n);
| ~ ^~
| )
In file included from place.cpp:1:
fourmis.hpp:9:5: error: 'Coord' does not name a type 9 | Coord coord();
| ^~~~~
fourmis.hpp:24:5: error: 'Coord' does not name a type
24 | Coord fCoord;
| ^~~~~
^~~~~~~~
place.hpp:6:16: error: expected ')' before 'x'
6 | Place(Coord x);
| ~ ^~
|
coord.hpp:
#ifndef COORD_HPP
#define COORD_HPP
#include <vector>
#include <string>
#include <array>
using namespace std
class Coord {
public:
Coord(); //constructor 1
Coord(int lig, int col); //constructor 2
};
#endif
ants.cpp
#include "coord.hpp"
#include "fourmis.hpp"
Fourmi::Fourmi(Coord a, int n){
fNum = n;
}
Coord Fourmi::coord(){
return fCoord;
}
int Fourmi::num(){
return fNum;
}
这是我的终端:
my.name@jupyterhub:~/Project$ make test
g++ -std=c++11 -Wall -c place.cpp
In file included from place.cpp:1:ants.hpp:6:17: error: expected ')' before 'a'
6 | Ants(Coord a, int n);
| ~ ^~
| )
In file included from place.cpp:1:
fourmis.hpp:9:5: error: 'Coord' does not name a type 9 | Coord coord();
| ^~~~~
fourmis.hpp:24:5: error: 'Coord' does not name a type
24 | Coord fCoord;
| ^~~~~
^~~~~~~~
place.hpp:6:16: error: expected ')' before 'x'
6 | Place(Coord x);
| ~ ^~
|
- 我跳过了“未在此范围内声明”错误。。。我的makefile很好,我想:如果有人能帮我的话,它肯定来自标题
- 当我尝试放置测试int ants.cpp(带doctest)时,它会显示“错误:预期的构造函数、析构函数或类型转换”。但当我在coord.cpp中尝试时,它是完全正确的
给能帮我的人一个免费的拥抱:)ants.h需要包含coord.h,否则
coord
不会在ants中声明。糟糕,我忘了把它放在我的主题中,但我已经在代码中做了,在bug之前(现在编辑了)你在源代码中添加了包含项,但是它们在标题中丢失了。我不确定是否完全理解:/include(和哪些?)ants应该放在哪个标题中。h需要包含coord.h,否则coord不会在ants.h中声明
my.name@jupyterhub:~/Project$ make test
g++ -std=c++11 -Wall -c place.cpp
In file included from place.cpp:1:ants.hpp:6:17: error: expected ')' before 'a'
6 | Ants(Coord a, int n);
| ~ ^~
| )
In file included from place.cpp:1:
fourmis.hpp:9:5: error: 'Coord' does not name a type 9 | Coord coord();
| ^~~~~
fourmis.hpp:24:5: error: 'Coord' does not name a type
24 | Coord fCoord;
| ^~~~~
^~~~~~~~
place.hpp:6:16: error: expected ')' before 'x'
6 | Place(Coord x);
| ~ ^~
|