2016-12-11 2 views
1

je continue à recevoir ces erreurs:C++ vraiment déroutant d'erreur reliant

cNeuralNetv2.obj : error LNK2019: unresolved external symbol "public: void __thiscall Matrix::set(int,int,double)" ([email protected]@@[email protected]) referenced in function _wmain

et

cNeuralNetv2.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Matrix::toStr(void)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) referenced in function _wmain

mon fichier d'en-tête ressemble à ceci:

class Matrix{ 
    private: 
     double** mat; 
     void makeInitArray(); 

    public: 
     int height, width; 
     double at(int, int); 
     void set(int, int, double); 
     void add(int, int, double); 
     string dimensions(); 
     string toStr(); 
     Matrix(int h, int w); 
}; 
Matrix dotMatrices(Matrix a, Matrix b); 

et ma matrice Le fichier .cpp ressemble à ceci:

#include "stdafx.h" 
#include <vector> 
#include <string> 
#include <iostream> 
#include <stdexcept> 
using namespace std; 

class Matrix{ 
private: 
    double** mat; 

    void makeInitArray(){ 
     mat = 0; 
     mat = new double*[height]; 
     for (int h = 0; h < height; h++) 
     { 
      mat[h] = new double[width]; 
      for (int w = 0; w < width; w++) 
      { 
       // fill in some initial values 
       mat[h][w] = 0.0; 
      } 
     } 
    } 

public: 
    int height, width; 

    double at(int x, int y){ 
     return mat[x][y]; 
    } 

    void set(int x, int y, double z){ 
     mat[x][y] = z; 
    } 

    void add(int x, int y, double z){ 
     mat[x][y] = z; 
    } 

    string toStr(){ 
     string output = ""; 
     for (int x = 0; x < height; x++){ 
      output += "[ "; 
      for (int y = 0; y < width; y++){ 
       output += to_string(mat[x][y]); 
       output += " "; 
      } 
      output += "]\n"; 
     } 
     return output; 
    } 

    string dimensions(){ 
     return to_string(height) + "x" + to_string(width); 
    } 

    Matrix(int h, int w){ 
     height = h; 
     width = w; 
     makeInitArray(); 
    } 
}; 

mon fichier principal ressemble à ceci:

#include "stdafx.h" 
#include <vector> 
#include <string> 
#include <iostream> 
#include <stdexcept> 

using namespace std; 

#include "mat.h" 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Matrix nMat(4, 3); 
    cout << nMat.toStr() << endl; 
    cout << nMat.dimensions() << endl; 
    nMat.add(0, 0, 1); 
    nMat.at(0, 0); 
    nMat.set(0, 0, 2); 
    int q; 
    cin >> q; 
    return 0; 
} 

je suis un noob à C++, mais je l'ai regardé ce pendant 4 jours, demander à des amis et personne ne semble avoir un answer

+0

avez-vous inclus 'string' dans le fichier d'en-tête? – Raindrop7

+2

Votre fichier matrix.cpp est faux, il déclare une autre classe différente appelée Matrix, qui est un comportement non défini (viole ODR). – immibis

+0

Vous ne déclarez pas la classe dans l'en-tête et le fichier source. Vous utilisez la syntaxe 'Matrix ::' dans le cpp pour écrire l'implémentation. – Carcigenicate

Répondre

3

Vous avez défini la classe Matrix deux fois: une fois dans l'en-tête et une fois dans le fichier .cpp. Vous voulez la définition de classe dans l'en-tête et les définitions de fonction de membre dans le fichier .cpp.

1

1- inclure une chaîne de classe dans l'en-tête de la classe Matrix tant que vous déclarez certains membres de la classe type.

2- faire l'en-tête class Matrix a seulement l'interface et dans le fichier source Matrix seulement la définition ne déclare pas à nouveau la classe Matrix.

votre Matrix.h ressemblera:

#include <string> // don't forget 

class Matrix 
{ 
    private: 
     double** mat; 
     int height, width; // declare member data private or protected an provide setters and getters instead of declaring them public 
     void makeInitArray(); 

    public:   
     void add(int, int, double); 
     // ... 
}; 

et la source Matrix.cpp Matrice ressemblera:

#include "matrix.h" 
// some other include headers here... 

void Matrix::makeInitArray() // don't write: void makeInitArray() directly 
{ 
    mat = 0; 
    mat = new double*[height]; 
    for (int h = 0; h < height; h++) 
    { 
     mat[h] = new double[width]; 
     for (int w = 0; w < width; w++) 
     { 
      // fill in some initial values 
      mat[h][w] = 0.0; 
     } 
    } 
} 

double Matrix::at(int x, int y) 
{ 
    return mat[x][y]; 
} 

continuer avec les définitions des autres fonctions membres comme ci-dessus.

+0

recherchez std :: to_string(). Il est défini dans le fichier d'en-tête de chaîne. C'est une sorte d'alias de std :: stoi ou atoi –