Проблема с шаблонным классом

Ответить

Код подтверждения
Введите код в точности так, как вы его видите. Регистр символов не имеет значения.

BBCode ВКЛЮЧЁН
[img] ВКЛЮЧЁН
[url] ВКЛЮЧЁН
Смайлики ОТКЛЮЧЕНЫ

Обзор темы
   

Развернуть Обзор темы: Проблема с шаблонным классом

Re: Makefile (LINUX)

Monopo » 20 апр 2009, 19:48

Romeo писал(а):...в котором методы имплементированы нету инстанцирования шаблона...
Да.. И вправду..
Romeo писал(а):Тебе нужно немного почитать о template'ах.
Спасибо))

Re: Makefile (LINUX)

Romeo » 16 апр 2009, 14:11

Тебе нужно немного почитать о template'ах. Дело в том, что у тебя не сгенерированы тела методов для <int> по той причине, что в файле, в котором методы имплементированы нету инстанцирования шаблона. Перемести код методов класса в header файл и у тебя всё соберётся.

Re: Makefile (LINUX)

Monopo » 15 апр 2009, 18:41

main.cpp:

Код: Выделить всё

#include "complex.h"
#include "massiv.h"

using namespace std;

int main()
{
        TMas *TM;
        int out=0;
        char c,type;
        printf("Choose the type of Massiv:\n");
        printf("0 - int\n");
        printf("1 - double\n");
        printf("2 - float\n");
        printf("3 - complex\n");
        scanf("%c",&type);
        switch(type)
        {
                case '0': {TM=new Massiv <int> ; break;}
                case '1': {TM=new Massiv <double>; break;}
                case '2': {TM=new Massiv <float>; break;}
                case '3': {TM=new Massiv <Complex>; break;}
        };
        while(out!=1)
	{
		printf("\n\nEnter the command:\n");
		printf("a - Add the massiv\n");
		printf("e - A[i]\n");
                printf("x - int(A)\n");
                printf("p - Print\n");
		printf("c - EXIT\n");
                printf("\n>>\n");
		cin>>c;
                printf("\n");
		switch (c)
		{
			case 'a':{TM->Input();break;}
                        case 'e':
                        {
                                int id;
                                printf("Enter the index> ");
                                scanf("%d",&id);
                                TM->PrintElem(id);
                                break;
                        }
                        case 'p':{TM->Print();break;}
                        case 'x':{TM->PrintCount();break;}
                        case 'c':
                        {
                                out++;
                                break;
                        }
                        default:continue;
		}
	}
        return 0;
}

Компилятор выдает кучу ошибок, типа:

Код: Выделить всё

main.o :( .rodata._ZTV6MassivIiE[vtable for Massiv<int>+0x1c]): undefined reference to 'Massiv<int>::Print()'
И так для каждой функции в TMas и для каждого типа, в том числе и пользовательского. 16 ошибок. Подскажите, где накосячил??

Проблема с шаблонным классом

Monopo » 15 апр 2009, 18:40

Пытаюсь собрать, да не получается.
Makefile:

Код: Выделить всё

MASSIV: complex.o massiv.o main.o
	g++ -o MASSIV complex.o massiv.o main.o //ошибка здесь при финальной сборке

main.o: main.cpp
	g++ -c main.cpp

massiv.o: massiv.cpp
	g++ -c massiv.cpp

complex.o: complex.cpp
	g++ -c complex.cpp

clean:
	rm -f *.o MASSIV
complex.cpp

Код: Выделить всё

#include <stdio.h>
#include <string.h>
#include "iostream"
#include <stdlib.h>

using namespace std;

class Complex
{
	public:
	int Re;
	int Im;
	Complex():Re(0),Im(0){};
	Complex(const int& tre,const int& tim):Re(tre),Im(tim){};
	Complex(const Complex&obj):Re(obj.Re),Im(obj.Im){};
	friend ostream& operator<<(ostream&,const Complex&);		
	friend istream& operator>>(istream&,const Complex&);
};

ostream& operator<<(ostream& out,const Complex& Tc)
{
	//..
}

istream& operator>>(istream& in,Complex& Tc)
{
	//..
}
complex.h:

Код: Выделить всё

#ifndef _COMPLEX_H_
#define _COMPLEX_H_

#include <stdio.h>
#include <string.h>
#include "iostream"
#include <stdlib.h>

using namespace std;

class Complex
{
	public:
	int Re;
	int Im;
	Complex():Re(0),Im(0){};
	Complex(const int& tre,const int& tim):Re(tre),Im(tim){};
	Complex(const Complex&obj):Re(obj.Re),Im(obj.Im){};
	friend ostream& operator<<(ostream&,const Complex&);		
	friend istream& operator>>(istream&,const Complex&);
};

        ostream& operator<<(ostream&,const Complex&);	
	
	istream& operator>>(istream&,const Complex&);
#endif //_COMPLEX_H_
massiv.cpp:

Код: Выделить всё

#include "complex.h"

class TMas
{
        public:
        virtual void Print(void)=0;
        virtual void PrintElem(const int& id)=0;
        virtual void Input(void)=0;
        virtual void PrintCount(void)=0;
};

template <class TType> class Massiv: virtual public TMas
{
	public:
        Massiv():Head(NULL),Last(NULL),count(0){};
        Massiv(const Massiv& ob):
        Head(ob.Head),Last(ob.Last),count(ob.count){};
        ~Massiv(){};
        void Input(void);
        void PrintElem(const int& id);
        void PrintCount();
        void Print(void);
	int operator()(Massiv<TType>& obj){return obj.count;};
	TType operator[](const int& id);
        protected:
	struct TT{
                TType item;
                TT*next;
        };
	int count;
	TT *Head;
	TT *Last;
};

template <class TType>
void Massiv<TType>::Input(void)
{
	//..
}

template <class TType>
void Massiv<TType>::Print(void)
{
	//..
}

template <class TType>
TType Massiv<TType>: :o perator[](const int& id)
{
	//..
}

template <class TType>
void Massiv<TType>::PrintElem(const int& id)
{
	//..
}

template <class TType>
void Massiv<TType>::PrintCount(void)
{
	//..
}
massiv.h:

Код: Выделить всё

#ifndef _MASSIV_H_
#define _MASSIV_H_

using namespace std;

class TMas
{
        public:
        virtual void Print(void)=0;
        virtual void PrintElem(const int& id)=0;
        virtual void Input(void)=0;
        virtual void PrintCount(void)=0;
};

template <class TType> class Massiv: virtual public TMas
{
	public:
        Massiv():Head(NULL),Last(NULL),count(0){};
        Massiv(const Massiv& ob):
        Head(ob.Head),Last(ob.Last),count(ob.count){};
        ~Massiv(){};
        void Input(void);
        void PrintElem(const int& id);
        void PrintCount();
        void Print(void);
	int operator()(Massiv<TType>& obj){return obj.count;};
	TType operator[](const int& id);
        protected:
	struct TT{
                TType item;
                TT*next;
        };
	int count;
	TT *Head;
	TT *Last;
};

#endif // _MASSIV_H_

Вернуться к началу