C++模板抽象继承

标签 c++ templates inheritance

我是计算机图形学硕士学位最后一年的学生,我们必须用 C++ 做一个练习。

代码如下:

AbsMatrice.h:

#pragma once

template<int M,int N, typename T>
class AbsMatrice
{
private:

T m_data[M][N];

public:
AbsMatrice();
~AbsMatrice();

AbsMatrice<M, N, T> mul(AbsMatrice<M,N,T> &a);
AbsMatrice<M, N, T> add(AbsMatrice<M, N, T> &a);
void read(...);

T& at(int row, int col);
T& operator ()(int row, int col);

//fonction virtuelle pure
virtual void print() = 0;

int& getNumRows(){ return M; }
int& getNumColumns(){ return N; }
};

矩阵.h:

#pragma once
#include "AbsMatrice.h"

template <int M, int N, typename T>

class Matrice : public AbsMatrice<M,N,T>
{

public:
    Matrice();
    ~Matrice();

    void print();
 };

AbsMatrice.cpp:

#include "AbsMatrice.h"
#include <iostream>


template<int M, int N, typename T>
AbsMatrice<M, N, T>::AbsMatrice(){
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            m_data[i][j] = 0;
        }
    }
}

template<int M, int N, typename T>
AbsMatrice<M, N, T>::~AbsMatrice()
{
}

template<int M, int N, typename T>
T& AbsMatrice<M, N, T>::operator ()(int row, int col)
{
    return m_data[row][col];
}

template<int M, int N, typename T>
T& AbsMatrice<M, N, T>::at(int row, int col)
{
    return m_data[row][col];
}

template<int M, int N, typename T>
AbsMatrice<M, N, T> AbsMatrice<M, N, T>::add(AbsMatrice<M, N, T> &a)
{
    if (this->getNumColumns() == a.getNumColumns() && this->getNumRows() == a.getNumRows())
    {
        for (int i = 0; i < M; i++)
        {
            for (int j = 0; j < N; j++)
            {
                m_data[i][j] += a(i, j);
            }
        }
    }
    else
        std::cout << "Erreur matrice de taille différentes !" << std::endl;

    return this;
}

template<int M, int N, typename T>
void AbsMatrice<M, N, T>::print()
{
    std::cout << "la matrice :" << std::endl;
}

矩阵.cpp:

#include "Matrice.h"
#include <iostream>


template <int M, int N, typename T>
Matrice<M,N,T>::~Matrice()
{
}

template <int M, int N, typename T>
void Matrice<M, N, T>::print()
{
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
            std::cout << " " << this->m_data[i][j] << " ";
        }
        std::endl;
    }
}

当我尝试在 main 中执行此操作时:

main.cpp:

#include "Matrice.h"


int main()
{
    Matrice<2,2,int> a;
    Matrice<2,2,int> b;

}

我很讨厌:

error C2259: 'AbsMatrice<2,2,T>' : cannot instantiate abstract class
1>          with
1>          [
1>              T=int
1>          ]
1>          due to following members:
1>          'void AbsMatrice<2,2,T>::print(void)' : is abstract
1>          with
1>          [
1>              T=int
1>          ]
1>          c:\users\bobmaza\documents\visual studio 2013\projects\tpmatrices\tpmatrices\absmatrice.h(22) : see declaration of 'AbsMatrice<2,2,T>::print'
1>          with
1>          [
1>              T=int
1>          ]

我试着在网上到处搜索,但没有找到接近我的案例,而且大多数人都没有使用相同的签名来覆盖抽象函数,但我的情况不是这样。我从未见过这样的错误,在此先感谢您。

错误消息的含义是什么以及如何更正它?

注意:几年来我一直在用 C++ 编写代码,但从来没有做过这样的事情,所以我想不通!

附言: 对不起,我是法国学生。

编辑:感谢大家的帮助,我第一次不得不问这里,很棒的社区!

最佳答案

您需要定义您的函数,而不仅仅是声明它:

virtual void print() {}

关于C++模板抽象继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33424228/

相关文章:

c++ - 模板中的默认参数 -> 模板参数涉及模板参数

c++ - 如何创建虚拟类的 vector ?

c++ - 如何确定模板非类型整数常量参数中的位数?

html - div 不继承 css(他们应该)

JavaScript 原型(prototype)继承

java - 出现 fatal error : jni. h : No such file or directory #include <jni. h>

c++ - C++中初始化列表的顺序

C++ 条件运算符与 if-else

c++ - 使用 makefile 编译 C++ 项目的问题

c++ - 从属名称的模板消歧器