Click here to Skip to main content
15,886,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I compile this code it gives me this error :
Unresolved external symbol _main referenced in function "int_cdecl invoke_main(void)"
what's the problem with my code?

What I have tried:

main function :
C++
#include "stdafx.h"
#include <iostream>
#include "myqueue.h"
using namespace std;
template <class T>
int main()
{
	myqueue <int> intQueue;
	cout <<"size ="<< intQueue.size() << endl;
	intQueue.add(10);
	intQueue.add(20);
	intQueue.add(30);
	cout << intQueue.retrieve() << endl;
	intQueue.remove();
	intQueue.add(40);
	intQueue.add(50);
	cout << intQueue.retrieve() << endl;
	intQueue.remove();
	intQueue.add(60);
	cout <<"size ="<< intQueue.size() << endl;
	while
		(!intQueue.isEmpty())
	{
		cout << intQueue.retrieve() << endl;
		intQueue.remove();
	}
	cout <<"size ="<< intQueue.size() << endl;
	return 0;
   
}



myqueue.h:

C++
#include<stack>
using namespace std;
template <class T>
class myqueue
{
private:stack <T> st1, st2;
public:
	myqueue();
	~myqueue();
	bool add(T element);
	T retrieve();
	bool remove();
	bool isEmpty();
	int size();
};


myqueue.cpp:

C++
#include "stdafx.h"
#include "myqueue.h"
#include <stack>
using namespace std; 

template <class T>
myqueue<T>::myqueue()
{
}
template <class T>
myqueue<T>::~myqueue()
{
}
template <class T>
bool myqueue<T>::add(T element) {
	st1.push(element);
	return true;
}
template <class T>
bool myqueue<T>::isEmpty() {
	if (st1.size()== 0) {
		cout << "Empty queue" << endl;
		return true;
	}
	cout << "Not empty queue" << endl;
	return false;
}
template <class T>
int myqueue<T>::size() {
	return st1.size();
}
template <class T>
T myqueue<T>::retrieve() {
	while (!st1.empty) {
		st2.push(st1.top);
		st1.pop();
	}
	T item = st2.top();
	while (!st2.empty) {
		st1.push(st2.top());
		st2.pop();
	}
	return item;
}
template <class T>
bool myqueue<T>::remove() {
	if (!st1.empty) {
		while (!st1.empty) {
			st2.push(st1.top);
			st1.pop();
		}

		while (!st2.empty) {
			st1.push(st2.top());
			st2.pop();
		}
		return true;
	}
	else return false;
}
Posted
Updated 7-Nov-16 5:34am
Comments
[no name] 5-Nov-16 13:01pm    
Your main function is not a template class.
Member.R 5-Nov-16 13:39pm    
I actually added : template <class T> before the main function because it gave me at first errors saying : unresolved external symbol "public_thiscall
myqueue<int>::myqueue<int>(void)
and the same for every function in the class
so when I thought adding template <class T> will solve the problem...
If you know how to solve this problem I would be really thankful for you !!
Albert Holguin 7-Nov-16 11:25am    
Your "fix" broke the expected main prototype. Your initial problem was signaling an error you attempted to fix incorrectly.

1 solution

Your main shouldn't be a template, that's your current error. The error you commented on above (in comments section) is telling you that the compiler doesn't see the implementations for the template instance, this is probably because you put that code in a cpp file instead of the header.

Here are some reference links to this issue:
c++ - Why can templates only be implemented in the header file? - Stack Overflow[^]
c++ template and header files - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900