WinFuture-Forum.de: C++ Linker Error - WinFuture-Forum.de

Zum Inhalt wechseln

Nachrichten zum Thema: Entwicklung
Seite 1 von 1

C++ Linker Error


#1 Mitglied ist offline   amdXP 

  • Gruppe: aktive Mitglieder
  • Beiträge: 84
  • Beigetreten: 15. März 06
  • Reputation: 0
  • Geschlecht:Männlich
  • Wohnort:Wien, Österreich
  • Interessen:Computer, Linux, Mac, Internet, Digitale Fotografie, Programmieren, Webdesign, und viele mehr ;-)

geschrieben 25. September 2010 - 14:08

Hi Leute. Bei einer Übungsaufgabe mit Templates und Stacks kamen diese Fehlermeldungen:

1>mainProg.obj : error LNK2019: unresolved external symbol "public: int __thiscall Stack<int>::pop(void)" (?pop@?$Stack@H@@QAEHXZ) referenced in function _main
1>mainProg.obj : error LNK2019: unresolved external symbol "public: void __thiscall Stack<int>::push(int)" (?push@?$Stack@H@@QAEXH@Z) referenced in function _main
1>C:\Users\oliver\documents\visual studio 2010\Projects\StackTemplate\Debug\StackTemplate.exe : fatal error LNK1120: 2 unresolved externals


Hier sind die codes:

Stack.h
#define NMAX 100

template <class T>
class Stack
{
private:
	T stack[NMAX];
	int stackPointer;
public:
	Stack() {stackPointer = 0;}
	void push(T value);
	T pop(void);
	void popall(void)
	{ stackPointer = 0; }
};


Stack.cpp
#include <iostream>
#include "Stack.h"
using namespace std;

template<class T>

void Stack<T>::push(T value)
{
	if(stackPointer < NMAX)
		stack[stackPointer++] = value;
	else
		cout << "PUSH: Stack is full" << endl;
}

template<class T>
T Stack<T>::pop()
{
	if(stackPointer > 0)
		return stack[--stackPointer];
	else
		cout << "POP: Stack is empty" << endl;
		
}


mainProg.cpp
#include <iostream>
#include <string>
#include "Stack.h"
using namespace std;

int main()
{

	int items;
	int i = 0;
	Stack<int> stapel;

	cout << "Legen Sie bitte 10 Zahlen auf den Stapel" << endl;

	while(i < 10)
	{
		cin >> items;
		stapel.push(items);
	}

	cout << "Sie haben diese Zahlen auf den Stapel gegeben" << endl;
	while(i < 10)
		cout << stapel.pop() << endl;

	cout << "Stapel wird geleert..." << endl;
	stapel.popall();
	
	cout << endl;

	return 0;
}


Was hab ich da was falsch gemacht?

Danke schon mal im Voraus
0

Anzeige



#2 Mitglied ist offline   Mr. Floppy 

  • Gruppe: VIP Mitglieder
  • Beiträge: 4.115
  • Beigetreten: 01. Juli 08
  • Reputation: 271
  • Geschlecht:Männlich

geschrieben 25. September 2010 - 18:16

Anders als bei normalem Code, darf man bei Templates Header und Implementierung nicht aufteilen. Packe mal beides in eine Datei, dann sollte es gehen.

http://www.cplusplus.com/doc/tutorial/templates/ sagte:

Templates and multiple-file projects

From the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.

When projects grow it is usual to split the code of a program in different source code files. In these cases, the interface and implementation are generally separated. Taking a library of functions as example, the interface generally consists of declarations of the prototypes of all the functions that can be called. These are generally declared in a "header file" with a .h extension, and the implementation (the definition of these functions) is in an independent file with c++ code.

Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.

Dieser Beitrag wurde von Mr. Floppy bearbeitet: 25. September 2010 - 18:16

0

#3 Mitglied ist offline   amdXP 

  • Gruppe: aktive Mitglieder
  • Beiträge: 84
  • Beigetreten: 15. März 06
  • Reputation: 0
  • Geschlecht:Männlich
  • Wohnort:Wien, Österreich
  • Interessen:Computer, Linux, Mac, Internet, Digitale Fotografie, Programmieren, Webdesign, und viele mehr ;-)

geschrieben 25. September 2010 - 19:11

Super, das hat geklappt!
Danke für die Hilfe!
0

Thema verteilen:


Seite 1 von 1

1 Besucher lesen dieses Thema
Mitglieder: 0, Gäste: 1, unsichtbare Mitglieder: 0