1

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 ?

2
  • This may violate the style rules under which you are programming, but the methods can be completely defined inside the class definition. For obvious and simple functions like int getYear() {return year}, putting the implementation in the class definition header can save you coding time and result in cleaner code. Commented Aug 16, 2015 at 17:24
  • And even create faster code through inlining Commented Aug 17, 2015 at 4:57

3 Answers 3

3

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

string Car::getName() {
    return name;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.