[英]How to use header functions in source file in C++?
I'm new to C++ programming language and it is different from Java. 我是C ++编程语言的新手,它与Java不同。 I tried to use functions from a header I made but when I use a function from the header , Eclipse C++ IDE says that member declaration is not found except for the constructor while it is found in the header as public.
我尝试使用我制作的标头中的函数,但是当我使用标头中的函数时,Eclipse C ++ IDE表示除构造函数外,未找到成员声明,而在标头中将其声明为public。
Car.h file (header) : 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 file (source) 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;
}
What I have did wrong ? 我做错了什么?
All of your functions are missing the return type, for example 您的所有函数都缺少返回类型,例如
string Car::getName() {
return name;
}
The reason why Car works is because it is a Constructor and does not need a type declaration. Car之所以起作用,是因为它是一个构造函数,不需要类型声明。
All the rest of your functions do. 您其余的所有功能都可以。
int Car::getYear() { // IDE says member declaration not found
return year;
}
Do this :- 做这个 :-
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.