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()'

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.