【发布时间】:2021-05-17 15:32:03
【问题描述】:
==> 我试图恢复一个很长的代码,所以除相关功能之外的所有其他功能都已经过测试并且正在运行!所有#include 也都可以
蚂蚁是:
#ifndef ANT_HPP
#define ANT_HPP
class Ant{
public:
Ant(Coord a, int n); //constructor: the coordonate of the ant, and its number
void deplace(Coord a); //an ant goes to coordonates a
Coord fCoord; //the coordonates of the Ant
int fNum; //a Number attributed to each ant };
#endif
所以,我在 Ant.cpp 中有这个:
void Ant::deplace(Coord a){ fCoord = a; }
并且测试(使用 doctest)运行良好:
TEST_CASE("test fourmis : "){
Ant f = Ant(Coord{2,3}, 3);
f.deplace(Coord{11,12});
CHECK(f.coord() == Coord{11,12}); }
在另一个文件中,我试图让 Ant 从 Place a 转到 Place b,如下所示:
void deplaceAnt(Ant f, Place a, Place b) {
p1.takeAnt(); //take an Ant away of a Place p1
f.deplace(p2.coord()); //the function that is not working :(
p2.putAnt(f); // put an Ant in a Place p2 }
place.hpp 就像:
#ifndef PLACE_HPP
#define PLACE_HPP
#include "Ant.hpp"
class Place{
public:
Place(Coord x); //constructor : create a Place from a Coordonate
int numAnt; //the number of the Ant on the Place
bool isAnt; //if there is an Ant or not
Coord Pcoord; //Coord of the Place }
#endif
这是不起作用的测试:
TEST_CASE("Place test : "){
Place q = Place(Coord{11,12});
Place p = Place(Coord{2,6});
Ant f = Ant(Coord{1,2}, 3)
q.putAnt(f);
deplaceAnt(f, q, p);
// f{1,2} must go from q{3,4} to p{2,6}
CHECK(f.fCoord == p.coord());
CHECK(q.isAnt == false);
CHECK(p.isAnt == true); }
这是编译后的消息:
TEST CASE: test Place :
place.cpp:206: ERROR: CHECK( f.fCoord == p.coord() ) is NOT correct!
values: CHECK( (1,2) == (2,6) )
place.cpp:207: ERROR: CHECK( q.isAnt == false ) is NOT correct!
values: CHECK( true == false )
place.cpp:209: ERROR: CHECK( p.isAnt == true ) is NOT correct!
values: CHECK( false == true )
===============================================================================
[doctest] test cases: 5 | 4 passed | 1 failed | 0 skipped
[doctest] assertions: 67 | 62 passed | 5 failed |
[doctest] Status: FAILURE!
我想知道为什么该函数在 file.cpp 中起作用,而在其他文件中不起作用。 +我在 Place.cpp 中重复使用的 Ant.cpp 中的其他函数(我没有在这里显示)并且有 np 与它们......很奇怪不是吗?
【问题讨论】:
-
您在
Ant标头中使用了相同的标头保护名称:#ifndef PLACE_HPP。考虑使用#pragma once,因为普通编译器支持它。 -
糟糕,我的 ctr+c/v 这部分代码很糟糕。 :o
标签: c++ makefile testcase doctest