没有匹配的函数调用仅在不同文件中创建对象时出现

时间:2015-05-23 07:24:23

标签: c++

我目前遇到的C ++中没有匹配的函数调用错误,如下所示

TestSuiteAssignment4.cpp: In function ‘int main()’:
TestSuiteAssignment4.cpp:533: error: no matching function for call to 
‘CreatureStack::CreatureStack(Creature*&)’
CreatureStack.hpp:32: note: candidates are: 
CreatureStack::CreatureStack(Creature*, CreatureStack*)
CreatureStack.hpp:31: note: CreatureStack::CreatureStack()
CreatureStack.hpp:18: note: CreatureStack::CreatureStack(const CreatureStack&)

奇怪的是,当我通过CreatureStack的实现文件中的main函数创建对象时,我没有得到任何这些错误。只有当我尝试在TestSuiteAssignment4.cpp文件中创建一个对象时才会出现此错误。

这是CreatureStack.hpp文件。

class CreatureStack
{
     private: 
         CreatureStack* next;
    CreatureStack* last;
    CreatureStack* first;
    Creature* item;

public:
    int count;
    Creature* remove();
    void add(Creature* item);
    bool empty();
    int returnCount();
    CreatureStack();
    CreatureStack(Creature* creature, CreatureStack* nextArg);

};

这是实现文件CreatureStack.cpp

中CreatureStack的构造函数
#include <iostream>
#include "CreatureStack.hpp"
#include "Creature.hpp"
#include "Goblin.hpp"


 /****************************************************************************
 ** Function: default constructor
 ** Description: constructs instacklist object
 ** Parameters: none
 ** Pre-conditions: no object created
 ** Post-conditions: object created
 ****************************************************************************/


CreatureStack::CreatureStack()
{
}



 /****************************************************************************
 ** Function: constructor
 ** Description: constructs object with arguments
 ** Parameters: int item, and pointer to next intstacklist object
 ** Pre-conditions: no object created
 ** Post-conditions: object created
 ****************************************************************************/



 CreatureStack::CreatureStack(Creature* creature, CreatureStack* nextArg = NULL)
 {
/*initiate object with the item argument, and a pointer to an IntStackList Object for Next */

item = creature;
next = nextArg;
count = 1;
last = this;
}

以下是CreatureStack实施文件的主要文件

int main()
{


Creature* test1 = new Goblin();
Creature* test2 = new Goblin();
Creature* test3 = new Goblin();

Creature* testPoint;

test1->setId(1);
test2->setId(2);
test3->setId(3);

CreatureStack stack1 = CreatureStack(test1);
stack1.add(test2);
stack1.add(test3);
}

当我运行这些代码行时,根本没有构造函数问题/编译问题。

只有当我创建一个新文件TestSuiteAssignment4.cpp时,才会遇到构造函数的编译错误。

在新文件中,我只有以下代码。

#include <iomanip>
#include <string>
#include <vector>
#include <cstdlib>
#include "Dice.hpp"
#include "CreatureStack.hpp"
#include "CreatureQueue.hpp"
#include "Creature.hpp"
#include "Goblin.hpp"

int main()
{

    Creature* test1 = new Goblin();
    Creature* test2 = new Goblin();
    Creature* test3 = new Goblin();

    Creature* testPoint;

    test1->setId(1);
    test2->setId(2);
    test3->setId(3);

    CreatureStack stack1 = CreatureStack(test1);
    stack1.add(test2);
    stack1.add(test3);

}

当我尝试编译TestSuiteAssignment4.cpp时,我得到的无匹配函数错误调用让我感到困惑,因为当我尝试在CreatureStack实现文件中创建一个CreatureStack对象时,我完全没有遇到任何问题。但是,当我将相同的代码放入TestSuiteAssignment4.cpp时,我得到了一个匹配的函数调用错误,该错误在实现文件中进行测试时从未出现过。

感谢您提供任何可能的帮助。

1 个答案:

答案 0 :(得分:2)

您应将默认值(下方)移至标题文件

CreatureStack::CreatureStack(Creature* creature, CreatureStack* nextArg = NULL)