Undefined referanse til noen funksjon selv når biblioteker er inkludert

stemmer
0

Jeg gjør et enkelt program for å beregne matriser. Det er 3 filer totalt, mat.h, mat.cppog main.cpp.

Innhold av mat.h:

#ifndef MAT_H
#define MAT_H

#include <array>

template<.....>
class Mat {
    ...
};

#endif

Innhold av mat.cpp:

#include mat.h

using std::array;

// Definitions of Mat functions/constructors

Innhold av main.cpp:

#include <iostream>
#include mat.h
using namespace std;

int main() {
    //Here I create a couple of Mat objects.
    return 0;
}

Problemet er at når jeg kompilere den ved hjelp g++ mat.cpp main.cppdet gir meg en linker feil, sier at alle de funksjonene som er definert i mat.cppikke er definert (udefinert referanse). Problemet forsvinner hvis jeg definerer den viktigste funksjonen i mat.cpp, men jeg ønsker å ha forskjellige filer.

For ordens skyld, her er feilmeldingen:

main.o: En la función `main':
main.cpp:(.text+0x42): referencia a `Mat<double, 1ul, 2ul>::Mat(std::array<double, 2ul> const&)' sin definir
main.cpp:(.text+0x6e): referencia a `Mat<double, 1ul, 2ul>::Mat(std::array<double, 2ul> const&)' sin definir
main.cpp:(.text+0x9a): referencia a `Mat<double, 2ul, 1ul>::Mat(std::array<double, 2ul> const&)' sin definir
main.cpp:(.text+0xc6): referencia a `Mat<double, 2ul, 1ul>::Mat(std::array<double, 2ul> const&)' sin definir
main.cpp:(.text+0x10c): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::Mat(std::array<Mat<double, 1ul, 2ul>, 2ul> const&)' sin definir
main.cpp:(.text+0x152): referencia a `Mat<Mat<double, 2ul, 1ul>, 2ul, 1ul>::Mat(std::array<Mat<double, 2ul, 1ul>, 2ul> const&)' sin definir
main.cpp:(.text+0x190): referencia a `Mat<int, 2ul, 2ul>::Mat(std::array<int, 4ul> const&)' sin definir
main.cpp:(.text+0x1c6): referencia a `Mat<int, 2ul, 2ul>::Mat(std::array<int, 4ul> const&)' sin definir
main.cpp:(.text+0x1fc): referencia a `Mat<int, 2ul, 2ul>::Mat(std::array<int, 4ul> const&)' sin definir
main.cpp:(.text+0x23e): referencia a `Mat<int, 2ul, 2ul>::Mat(std::array<int, 4ul> const&)' sin definir
main.cpp:(.text+0x254): referencia a `Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>::Mat(std::array<Mat<int, 2ul, 2ul>, 4ul> const&)' sin definir
main.cpp:(.text+0x2e7): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o: En la función `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul> mult<double, int, 1ul, 2ul, 1ul, 2ul, 2ul, 2ul>(Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>, Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>)':
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0x3a): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::Mat()' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0x8d): referencia a `Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0xb9): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0xfc): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0x10e): referencia a `Mat<double, 1ul, 2ul>& Mat<double, 1ul, 2ul>::operator+=<double>(Mat<double, 1ul, 2ul>)' sin definir
main.o: En la función `Mat<double, 1ul, 2ul> mult<double, int, 1ul, 2ul, 2ul>(Mat<double, 1ul, 2ul>, Mat<int, 2ul, 2ul>)':
main.cpp:(.text._Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE[_Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE]+0x2b): referencia a `Mat<double, 1ul, 2ul>::Mat()' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE[_Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE]+0x77): referencia a `Mat<double, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE[_Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE]+0x97): referencia a `Mat<int, 2ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE[_Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE]+0xbe): referencia a `Mat<double, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIS_IiLm2ELm2EELm2ELm2EE[_ZTV3MatIS_IiLm2ELm2EELm2ELm2EE]+0x10): referencia a `Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIS_IiLm2ELm2EELm2ELm2EE[_ZTV3MatIS_IiLm2ELm2EELm2ELm2EE]+0x18): referencia a `Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>::set(Mat<int, 2ul, 2ul> const&, unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIiLm2ELm2EE[_ZTV3MatIiLm2ELm2EE]+0x10): referencia a `Mat<int, 2ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIiLm2ELm2EE[_ZTV3MatIiLm2ELm2EE]+0x18): referencia a `Mat<int, 2ul, 2ul>::set(int const&, unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIS_IdLm1ELm2EELm1ELm2EE[_ZTV3MatIS_IdLm1ELm2EELm1ELm2EE]+0x10): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIS_IdLm1ELm2EELm1ELm2EE[_ZTV3MatIS_IdLm1ELm2EELm1ELm2EE]+0x18): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::set(Mat<double, 1ul, 2ul> const&, unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIdLm2ELm1EE[_ZTV3MatIdLm2ELm1EE]+0x10): referencia a `Mat<double, 2ul, 1ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIdLm2ELm1EE[_ZTV3MatIdLm2ELm1EE]+0x18): referencia a `Mat<double, 2ul, 1ul>::set(double const&, unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIdLm1ELm2EE[_ZTV3MatIdLm1ELm2EE]+0x10): referencia a `Mat<double, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIdLm1ELm2EE[_ZTV3MatIdLm1ELm2EE]+0x18): referencia a `Mat<double, 1ul, 2ul>::set(double const&, unsigned long, unsigned long)' sin definir
collect2: error: ld returned 1 exit status
Publisert på 19/09/2018 klokken 13:24
kilden bruker
På andre språk...                            

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more