【发布时间】:2017-01-06 06:19:14
【问题描述】:
我正在尝试将对象放入数组列表中,然后弹出 3 个错误。我查看了论坛,有一个类似于我的question,但我认为它不适用于我的情况。 这是我的代码:
在 test.cpp(主文件)中
#include <iostream>
#include "House.h"
using namespace std;
House HouseArray[2];
int main()
{
string toPrint;
House Kubo("Kubo", 2);
HouseArray[0] = Kubo;
toPrint = HouseArray[0].GetHouseName;
cout <<toPrint<< endl;
}
在 House.cpp 中
#include "House.h"
#include <iostream>
House::House(string a, int h)
{
Name = a;
Health = h;
}
void House::DamageHouse(int d) {
Health -= d;
cout << "Your " << Name << " has " << Health << " left."<<endl;
}
int House::GetHouseHealth() {
return Health;
}
string House::GetHouseName() {
string returning = Name;
return returning;
}
House::~House()
{
}
在House.h
#include <string>
using namespace std;
class House
{
string Name;
int Health;
public:
House(string a, int h);
int GetHouseHealth();
void DamageHouse(int d);
string GetHouseName();
~House();
};
错误:
- 错误 C2512 'House':第 9 行中没有合适的默认构造函数可用 test.cpp
- 错误 C3867 'House::GetHouseName':非标准语法;使用“&”来 创建一个指向 第 16 行的成员 test.cpp
- 错误 C2679 二进制“=”:未找到采用右手的运算符 'overloaded-function' 类型的操作数(或者没有可接受的 转换)第 16 行中的 test.cpp
【问题讨论】:
-
你打算调用
GetHouseName成员函数吗?而且当你创建一个对象数组时,数组中的元素将是默认构造的,你没有默认构造函数。