Hi there,

I'm trying to return base class pointers from the two classes 'base' and 'child', but I keep on getting a build error of "1 unresolved externals".

I think it may be because I am linking all the files together incorrectly.

When I comment out the problem line in main.cpp it builds fine, but when I try to use it (and return a base class pointer), this is when I cannot build the program.

I have copy and pasted my code exactly here:

child.cpp :

#include "base.h"
#include "child.h"
#include <iostream>
#include <string>
using namespace std;

//Child class contructor
Child::Child(string basestring, string childstring) : Base(basestring)
{
	_basestring = basestring;
	_childstring = childstring;
}

//Child class function
//Return's a base class pointer
Child * ReturnPtrToANewChildClassObject()
{
	//Build an child class object, and then return a pointer to that object
	//Wthin main() the pointer will be be pushed into the std:list

	string basestring;
	cout << "Base string ? " << endl;
	getline (cin, basestring);

	string childstring;
	cout << "Child string ? " << endl;
	getline (cin, childstring);

	//Get a pointer to the new object
	Child *ptr = new Child(basestring, childstring);

	//return the pointer to main()
	return (ptr);
}

base.cpp :

#include "base.h"
#include "child.h"
#include <iostream>
#include <string>
using namespace std;

//Base class contructor
Base::Base(string basestring)
{
	_basestring = basestring;
}

//Base class function
Base * ReturnPtrToANewBaseClassObject()
{
	//Build an object, and then return a pointer to that object
	//Wthin main() the pointer will be be pushed into the std:list

	string basestring;
	cout << "Value? " << endl;
	getline (std::cin, basestring);

	//Get a pointer to the new object
	Base *ptr = new Base(basestring);

	//return the pointer to main()
	return (ptr);
}

child.h :

#ifndef CHILD_H
#define CHILD_H

#include <iostream>
#include <string>
using namespace std;

class Child : public Base
{
public:

	//constructor
	Child(string basestring, string childstring);

	//Member Functions:
	static Child * ReturnPtrToANewChildObject();//Create an child object and return a pointer to it

private:

protected:

	//Data Members specific to child class objects (concrete class):
	string _childstring;
};
#endif

base.h :

#ifndef BASE_H
#define BASE_H

#include <iostream>
#include <string>
using namespace std;

class Base
{
public:

	//base class constructor
	Base(string basestring);

	//Member Functions:
	static Base * ReturnPtrToANewBaseClassObject();//Create a base class object and return a pointer to it

private:

protected:

	//Data Members common to all classes (abstract base class):
	string _basestring;
};
#endif

main.cpp :

#include <iostream>
#include <list>
#include "base.h"
#include "child.h"
using namespace std;
int main()
{
	list<Base *>BaseList;

	do{
		char choice;
		cout << "a) Create then add a base class pointer into the list" << endl;
		cin >> choice;
		cin.ignore();

		switch(choice)
		{
		case 'a':
			{

				//Create an base object and return a pointer to that object
				Base *ptr = Base::ReturnPtrToANewBaseClassObject();

				//IT'S THE LINE ABOVE STOPPING THE PROGRAM COMPILING
				
				//	fatal error LNK1120: 1 unresolved externals



				//then push_back into the std::list
				//BaseList.push_back( ptr );
				cout << "Object now pushed onto the std::list" << endl;
				break;
			}
		default:
			{
				cout << "You made an invalid choice" << endl;
				break;
			}
		}
	}
	while(true);
	system("pause");
	return 0;
}

Visual Studio say's:

------ Build started: Project: deleteMe2, Configuration: Debug Win32 ------
Compiling...
main.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: static class Base * __cdecl Base::ReturnPtrToANewBaseClassObject(void)" (?ReturnPtrToANewBaseClassObject@Base@@SAPAV1@XZ)
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\deleteMe2\Debug\deleteMe2.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\deleteMe2\deleteMe2\Debug\BuildLog.htm"
deleteMe2 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I want to be able to create and store pointers to both base class objects and child objects, but right now this is a problem I cannot seem to solve.

Could anyone point out where my problem lies?

Thanks very much for any help!

It really is most appreciated.

Carrots

>>Base * ReturnPtrToANewBaseClassObject()
In base.cpp you forgot to add the Base:: class scope Base * Base::ReturnPtrToANewBaseClassObject()

commented: Thanks ever so much for your help! +1
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.