找到你要的答案

Q:How to use header functions in source file in C++?

Q:如何在C++源文件头的功能?

I'm new to C++ programming language and it is different from 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.

Car.h file (header) :

#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)

#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 ?

我是新来的C++编程语言,它不同于java。我试图用我但是当我使用一个函数从头球功能,Eclipse C++ IDE说成员声明不存在构造函数除外,它是在找到公共。

汽车文件(标题):

#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;
}

我做错了什么?

answer1: 回答1:

All of your functions are missing the return type, for example

string Car::getName() {
    return name;
}

所有函数都缺少返回类型,例如

string Car::getName() {
    return name;
}
answer2: 回答2:

The reason why Car works is because it is a Constructor and does not need a type declaration.

All the rest of your functions do.

int Car::getYear() { // IDE says member declaration not found
    return year;
}

汽车之所以工作,是因为它是一个构造函数,不需要类型声明。

其余的函数都做。

int Car::getYear() { // IDE says member declaration not found
    return year;
}
answer3: 回答3:

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;
}

这样做:—

#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;
}
c++  eclipse  declaration