0

I tried to make a template class representing a matrix with linked lists, but this linker error keeps coming up and I'm out of hope.

This is the error code i'm getting:

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __cdecl Matrix5Diagonal::Matrix5Diagonal(int *,int)" (??0?$Matrix5Diagonal@H@@QEAA@PEAHH@Z) referenced in function main

This is the problematic segment of my code My header file:

#pragma once

template <class T>
class Node;

template <class T>
class Matrix5Diagonal
{
        int size;
        Node<T>& origin;

public:
        Matrix5Diagonal(T* matrix, int psize);

        Node<T>& findNode(int x, int y);
};

template <class T>
class Node : public Matrix5Diagonal<T>
{
        T data = 0;
        int row;
        int column;
        Node<T>& down = nullptr;
        Node<T>& right = nullptr;

public:
        Node(T data, int row, int column);
        ~Node();

        Node<T>& getRight() { return right; }
        Node<T>& getDown() { return down; }
        int getRow() { return row; }
        int getColumn() { return column; }
        void setRow(int a) { row = a; }
        void setColumn(int b) { column = b; }
        void setData(int c) { data = c; }
};

And my cpp:

#include "Matrix5Diagonal.h"

#include <iostream>

template <class T>
Matrix5Diagonal<T>::Matrix5Diagonal(T* matrix, int psize) {
    origin = new Node<T>(0, 0, 0);
    Node<T>& step = origin;
    Node<T>& rowStart = origin;
    size = psize;
    int i, j;

    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            step.getRight() = new Node<T>(matrix[i * size + j], i, j);
            step = step.getRight();
            step.getRight() = origin;
        }
        step = step.getRight();
        i++;
        step.getDown() = new Node<T>(matrix[i * size + j], i, j);
        i--;
        step = step.getDown();
        step.getDown() = origin;
    }

    step = origin;
    for (j = 1; j < size; j++) {
        for (i = 0; i < size; i++) {
            findNode(i, j).getDown = findNode(i + 1, j);
            findNode(i + 1, j).getDown = findNode(0, j);
        }
    }
}

template <class T>
Node<T>& Matrix5Diagonal<T>::findNode(int x, int y) {
    int i, prev;
    if (x > size) x = size;
    if (y > size) y = size;
    Node<T>& step = origin;
    prev = -1;
    for (i = 0; i > prev && i <= x; prev = i) {
        step = step.getDown();
        i = step.row;
    }
    prev = -1;
    for (i = 0; i > prev && i <= y; prev = i) {
        step = step.getRight();
        i = step.column;
    }

    return step;
}

template <class T>
Node<T>::Node(T data, int row, int column) {
    this->data = data;
    this->row = row;
    this->column = column;
    down = nullptr;
    right = nullptr;
}

So far I tried putting everything in a .hpp file, but no luck so far.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 3
    _"I tried putting everything in a .hpp"_ - You should put the template implementations in the header file, yes. What didn't work out when you did that? Please show _that_ code instead. – Ted Lyngmo May 31 '23 at 20:09
  • 2
    Apparently OP tried to put everything in a header file. I'm curious about what the problem was with that. – Ted Lyngmo May 31 '23 at 20:16
  • 1
    That would be a better question. I can remove my duplicate request if we see the header only implementation and header only errors. – drescherjm May 31 '23 at 20:17
  • 2
    *"I tried putting everything in a .hpp file, but no luck so far."* unfortunately isn't a suitable description of the version of the code/error in that scenarios, so for now it seems like the only reasonable option here is to close it as dupe regardless of this one sentence... – fabian May 31 '23 at 20:24
  • 1
    Always make sure the error doesn't change. I've seen too many people throw out a valid solution for problem A because it didn't also solve the as-yet unrecognized problem B. – user4581301 May 31 '23 at 20:27

0 Answers0