I am sure there is nothing wrong with this... I am racking my brain as to why it wont compile.

#include <iostream>
#include <string>

using namespace std;

class Vehicle {
	public:
		Vehicle();
		Vehicle(string m, int y);
		void print() const;
	protected:
		string model;
		int year;
};

void Vehicle::print() const {
	cout << "Model = " << model << " , year = " << year << endl;
}

int main() {
	Vehicle a; //something extremely basic is moaning about this line
                   //if i use Vehicle a(); it seems to work but the rest of it has                                //problems because i used the parenthesis
	a.print();	
}

the error is:

C:/Documents and Settings/Daryl/My Documents/159234/study1/inheritance.cpp:22: undefined reference to `Vehicle::Vehicle()'

Dani AI

Generated

As already hinted: the linker error "undefined reference to Vehicle::Vehicle()" means you declared a default constructor inside the class but never provided its definition. The compiler accepted the class declaration, but the linker could not find the constructor body to produce an object.

Provide a definition (and initialize members) or remove the need for a default constructor. For example, define a safe default constructor that initializes model and year:

Vehicle::Vehicle() : model("Unknown"), year(0) {}

Or, in C++11 and later, explicitly request the compiler-generated default constructor if that matches your intent:

Vehicle() = default;

If you intended to put the constructor body in a separate .cpp file, make sure that file is compiled and linked with the rest of the program (for example: g++ -std=c++11 main.cpp vehicle.cpp -o program). A common cause of "undefined reference" is forgetting to link the translation unit that contains the definition — see this explanation on linker errors (Stack Overflow).

One more pitfall to note (this explains why Vehicle a(); "seemed to work"): Vehicle a(); is not an object construction — it is a function declaration (the "most vexing parse"). Use Vehicle a; to create a default object, or supply parameters: Vehicle a("Model", 2010);. For details see the C++ references on default constructors and the most vexing parse: default constructor and .

Summary checklist:

  • Define the declared Vehicle() or remove/replace it.
  • Initialize built-in members (like int year) to avoid garbage.
  • If the definition is in another file, compile/link that file.

Recommended Answers

All 2 Replies

It sounds like your compiler wants you to define the default constructor since you declared it and called it. The easiest response to this would be to add a set of curly brackets after the declaration of the default constructor and rebuild/run the program again.

Note that your program doesn't have the default constructor give the member variables any meaningful default values so calling print() object a will give junk for output, though it shouldn't stop compilation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.