【问题标题】:function not taken into account (w. makefile)未考虑功能(w.makefile)
【发布时间】: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


【解决方案1】:

你的功能

void deplaceAnt(Ant f, Place a, Place b)

按值获取参数,这意味着它们是副本。对这些对象的更改不会影响原始对象。您需要通过引用传递它们:

void deplaceAnt(Ant& f, Place& a, Place& b)

因此,您是在更改原始对象而不是副本。

【讨论】:

  • 编译时出现以下错误:error: call of overloaded 'deplaceAnt(Ant&, Place&, Place&)' is ambiguous
  • @piau 我假设您只有一个 deplaceAnt 函数。您是否更改了标头和 cpp 中的功能?您需要在任何地方进行更改。
  • 哦,是的,它终于可以工作了,非常感谢!你帮了我很多:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 2014-12-25
  • 2015-03-19
  • 1970-01-01
相关资源
最近更新 更多