我是C++编程语言的新手,它与Java语言不同。我尝试使用我创建的头中的函数,但是当我使用头中的函数时,Eclipse显示除了构造函数之外没有找到成员声明,而在头中发现它是公共的。
Car.h文件(表头):
#include <string>
using namespace std;
class Car {
private :
string name;
string model;
int year;
int width;
int height;
int depth;
public :
Car ();
Car (string n, string m, int y, int w, int h, int d);
void setName(string n);
void setModel (string m);
void setYear (int y);
void setSize (int w, int h, int d);
string getName ();
string getModel();
int getYear();
int getWidth();
int getHeight();
int getDepth();
};
Car.cpp文件(源)
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
Car::Car(string n, string m, int y, int w, int h, int d) { //works properly
name = n;
model = m;
year = y;
width = w;
height = h;
depth = d;
}
Car::getName() { // IDE says member declaration not found
return name;
}
Car::getModel() { // IDE says member declaration not found
return model;
}
Car::getYear() { // IDE says member declaration not found
return year;
}
Car::getWidth() { // IDE says member declaration not found
return width;
}
Car::getHeight () { // IDE says member declaration not found
return height;
}
我做错了什么?
发布于 2015-08-16 15:31:36
您的所有函数都缺少返回类型,例如
string Car::getName() {
return name;
}
发布于 2015-08-16 15:33:17
Car之所以能工作,是因为它是一个构造函数,不需要类型声明。
其余的函数都是这样做的。
int Car::getYear() { // IDE says member declaration not found
return year;
}
发布于 2015-08-16 15:42:29
执行此操作:-
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
Car::Car(string n, string m, int y, int w, int h, int d) { //works properly
name = n;
model = m;
year = y;
width = w;
height = h;
depth = d;
}
string Car::getName() { // IDE says member declaration not found
return name;
}
string Car::getModel() { // IDE says member declaration not found
return model;
}
int Car::getYear() { // IDE says member declaration not found
return year;
}
int Car::getWidth() { // IDE says member declaration not found
return width;
}
int Car::getHeight () { // IDE says member declaration not found
return height;
}
https://stackoverflow.com/questions/32036817
复制相似问题