我收到此错误 错误C2511:'printlocation':在'creature'中找不到重载的成员函数'int(void)'
这是代码: Location.h
#ifndef location_h
#define location_h
class location
{
public:
setpoint(int,int,int);
getpoint(int,int,int);
private:
int x,y,z;
};
#endif
.cpp代码如下:
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include"location.h"
location::getpoint(int a,int b,int c)
{
x=a;
y=b;
z=c;
cout<<"give point x"<<x<<endl;
cout<<"give point y"<<y<<endl;
}
生物类代码:
#ifndef creature_h
#define creature_h
#include"location.h"
#include<string>
//using std::string;
using namespace std;
class creature
{
public:
creature();
moveto(location l);
getname(string n);
printlocation(string ,location );
private:
location lo;
string name;
};
#endif
其中存在错误的creature.cpp代码:
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
using namespace std;
#include<string>
#include"creature.h"
#include"location.h"
creature::creature()
{
lo;
name;
}
creature::getname(string n)
{
name=n;
cout<<"enter name";
cin>>n;;
}
creature::printlocation()
:name(n),lo(l)
{
name=n;
lo=l;
cout<<lo.setpoint(int,int,int);
}
-----仍然存在错误....---
#ifndef creature_h
#define creature_h
#include"location.h"
#include<string>
//using std::string;
using namespace std;
class creature
{
public:
creature();
void moveto(location l);
void getname(string );
void printlocation(string ,location );
private:
location lo;
string name;
};
#endif
的.cpp
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
using namespace std;
#include<string>
#include"creature.h"
#include"location.h"
/*creature::creature()
{
lo;
name;
}*/
void creature::getname(string n)
{
name=n;
cout<<"enter name";
cin>>n;
}
void creature::printlocation()
:name(n),lo(l)
{
name=n;
lo=l;
cout<<l.getpoint(int,int,int);
}
的main.cpp
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
#include"location.h"
#include"creature.h"
#include<string>
main()
{
string n;
int x,y,z;
location l;
l.getpoint(x,y,z);
creature c;
c.getname(n);
c.printlocation(n,l);
return 0;
}
答案 0 :(得分:2)
函数声明必须指定它的返回类型(以及参数的正确命名空间,因为我没有看到使用命名空间std):
void printlocation( std::string ,location );
为其他声明添加相同内容:
void creature();
void moveto(location l);
void getname(string n);
然后以这种方式致电printlocation
:
std::string s;
location l;
//... give some value to s and l ...
printlocation( s, l);
同时更改其名称以符合cammel案例:printLocation
,更好。
答案 1 :(得分:0)
所有成员函数声明都没有返回类型。例如
class location
{
public:
setpoint(int,int,int);
getpoint(int,int,int);
//...
OT
class creature
{
public:
//...
moveto(location l);
getname(string n);
printlocation(string ,location );
//...
您必须指定函数的返回类型。
例如
void setpoint(int,int,int);
此外,函数getpoint在逻辑上更多地对应于函数设定点,因为它设置了对象的数据成员的值
location::getpoint(int a,int b,int c)
{
x=a;
y=b;
z=c;
cout<<"give point x"<<x<<endl;
cout<<"give point y"<<y<<endl;
}
所以我将该函数重命名为setpoint。例如
void location::setpoint(int a,int b,int c)
{
x=a;
y=b;
z=c;
cout<<"give point x"<<x<<endl;
cout<<"give point y"<<y<<endl;
}
答案 2 :(得分:0)
修复现有答案中提到的声明错误后,在.cpp
文件中为所有定义提供与声明中使用的签名相同的签名:
// NOTE the parameter defintions!!
void creature::printlocation(string n,location l) {
name=n;
lo=l;
cout<<lo.setpoint(1,2,3);
}
还请注意,成员初始值设定项语法仅对构造函数中的用法有效:
void creature::printlocation() : name(n),lo(l)
// ^^^^^^^^^^^^^^^ This is wrong!!!
<强>更新强>
您可能打算为creature
class creature {
public:
creature() (string n,location l) : name(n),lo(l) {}
// ...
};