Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Dzielenie kodu na kilka plików źródłowych- szablony

Ostatnio zmodyfikowano 2013-01-05 22:09
Autor Wiadomość
kopek11111
Temat założony przez niniejszego użytkownika
Dzielenie kodu na kilka plików źródłowych- szablony
» 2013-01-05 21:32:33
Witam, na pisałem sobie prosty programik w szablonach klas C++ i chciałbym go rozdzielić na kilka plików lecz dostaję takie błędy:

1>Kalkulator.obj : error LNK2019: unresolved external symbol "public: int __thiscall Calculator<int>::add(int,int)" (?add@?$Calculator@H@@QAEHHH@Z) referenced in function _wmain
1>Kalkulator.obj : error LNK2019: unresolved external symbol "public: __thiscall Calculator<int>::Calculator<int>(void)" (??0?$Calculator@H@@QAE@XZ) referenced in function _wmain
1>M:\Kalkulator.exe : fatal error LNK1120: 2 unresolved externals

Plik główny (cpp):

C/C++
#include "stdafx.h"
#include <iostream>
#include "Calculator.h"
using namespace std;
int _tmain( int argc, _TCHAR * argv[] )
{
    Calculator < int > nowy;
    cout << nowy.add( 2, 43 );
    return 0;
}

Plik nagłówkowy - Calculator.h

C/C++
#ifndef Calculator_h
#define Calculator_h

template < class __Value >
class Calculator {
public:
    Calculator();
    __Value add( __Value, __Value );
    __Value remove( __Value, __Value );
    __Value multiply( __Value, __Value );
    __Value divide( __Value, __Value );
};

#endif

Plik Calculator.cpp

C/C++
#include "StdAfx.h"
#include "Calculator.h"
#include <iostream>
template < class __Value >
Calculator < __Value >::Calculator()
{
    std::cout << "The calculator is opened correct" << std::endl;
}
template < class __Value >
__Value Calculator < __Value >::add( __Value x, __Value y )
{
    return x + y;
}
template < class __Value >
__Value Calculator < __Value >::remove( __Value x, __Value y )
{
    return x - y;
}
template < class __Value >
__Value Calculator < __Value >::multiply( __Value x, __Value y )
{
    return x * y;
}
template < class __Value >
__Value Calculator < __Value >::divide( __Value x, __Value y )
{
    return x / y;
}

Dodam, że jeśli nie zrobię tego w szablonie to działa wiecie może w czym jest problem?
P-73099
DejaVu
» 2013-01-05 22:01:37
Szablony nie mogą zostać skompilowane do plików *.lib itp., więc ich implementacja musi być w pliku nagłówkowym.
P-73103
kopek11111
Temat założony przez niniejszego użytkownika
» 2013-01-05 22:09:42
Thank you very much ;) Temat do zamknięcia.
P-73104
« 1 »
  Strona 1 z 1