C ++ error LNK2019 Unresolved external symbol

2

I have an error and I do not know how to solve it, extra information is: Visual studio 2017, in an empty project.

  

LNK2019 unresolved external symbol "public: static void __cdecl   Bisection :: start (void) "(? Home @ Biseccion @@ SAXXZ) referenced in   function _main

Source.cpp:

#include "Biseccion.h"
using namespace std;
int main() {
    //Biseccion * bi = new Biseccion();
    Biseccion bi;
    bi.inicio();
    system("PAUSE");
    return 0;
}

Biseccion.h:

#pragma once
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include "Ecuacion.h"

using namespace std;
class Biseccion {
public:
    static void inicio(void);
    Biseccion(void);
};

Biseccion.cpp:

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

using namespace std;
class Biseccion {
private:
    int longitudEcua = 0;
    Ecuacion * ecuat;
    vector<double> * preguntarArray() {
//codigo del metodo
        return &ecu;
    }

public:
    void inicio(void) {
//Codigo del metodo
    }
    Biseccion();
};

Biseccion::Biseccion() { ; }

Ecuacion.h:

#pragma once
#include <vector>
#include <math.h>

using namespace std;
class Ecuacion {
public:
    void setEcua(vector<double> * s);
    vector<double>* getEcua(void);
    Ecuacion(int);
    double getPosicion(int x);
    void setPosicion(int x, double val);
    double calculaEcua(double x);
};

Ecuacion.cpp:

#include <vector>
#include <math.h>

using namespace std;
class Ecuacion {
private:
    int siseX;
    vector<double>  ecua;
public:

    void setEcua(vector<double> * s) { ecua = *s; };
    vector<double>* getEcua() { return &ecua; }
    Ecuacion(int);

    double getPosicion(int x) {
        return ecua.at(x);
    };
    void setPosicion(int x, double val) {
        ecua.insert(ecua.begin() + x, val);
    }
    double calculaEcua(double x) {
//codigo del metodo
};

Ecuacion::Ecuacion(int x) {
//constructor
}
    
asked by francisco5em 26.10.2018 в 03:11
source

2 answers

3

In C ++ it is not necessary to use void as a parameter

In C, if we do not put void to a function that does not receive parameters, it can receive an indeterminate number of them:

void func1();     // func admite parametros: func1(1,2,3,4,5);
void func2(void); // func2 NO admite parametros. solo func2() es llamada valida

However in C ++ it is not like that. If a function has no parameters, no will allow parameters. So since void surplus, do not use it. You will leave the code cleaner.

// Evitar esto
class Biseccion {
public:
    static void inicio(void);
    Biseccion(void);
};

// Mejor asi
class Biseccion {
public:
    static void inicio();
    Biseccion();
};

Do not use using namespace in header files

More information here: Why using "using namespace std;" Is it considered bad practice?

The declaration of each class must be unique

The code you currently display has two statements of each class:

Biseccion.h:

using namespace std;
class Biseccion { // <<--- DECLARACION
public:
    static void inicio(void);
    Biseccion(void);
};

Biseccion.cpp:

class Biseccion { // <<--- DECLARACION
private:
    int longitudEcua = 0;
    Ecuacion * ecuat;
    vector<double> * preguntarArray() {
//codigo del metodo
        return &ecu;
    }

And I regret to inform you that this will not work in life. The declaration of a class must be unique, that is, you can only have one in the whole program. In addition, that statement must be complete , that is, it must include all the variables and functions of the class.

The reason is simple: The compiler needs to know, from the beginning, the requirements of a class (size in memory, location of each variable, functions that are static, builders available, ...).

Using the class Biseccion as a sample, your disposition could be as follows:

Biseccion.h

class Biseccion {
private:
    int longitudEcua = 0;
    Ecuacion * ecuat;

    vector<double> * preguntarArray();

public:
    void inicio();
    Biseccion();
};

Biseccion.cpp

#include "Biseccion.h"

Biseccion::Biseccion()
{ /* ... */ }

void Biseccion::inicio()
{ /* ... */ }

vector<double>* preguntarArray()
{ /* ... */ } // <<--- ecu no existe
    
answered by 26.10.2018 / 08:17
source
1

The linker is telling you that you have declared the function static void Biseccion::inicio(void) , but you have not defined it, you can see more details in this thread .

In your header ( .h ) you declare the function as static:

class Biseccion {
public:
    static void inicio(void); // Estática
    Biseccion(void);
};

In your code file ( .cpp ) you define the function as a member:

class Biseccion {
private:
    int longitudEcua = 0;
    Ecuacion * ecuat;
    vector<double> * preguntarArray() {
//codigo del metodo
        return &ecu;
    }

public:
    void inicio(void) { // Función miembro, ¡no función estática!
//Codigo del metodo
    }
    Biseccion();
};

For the compiler the static version and the member version are different functions, so you have a function static void Biseccion::inicio(void) declared but the defined part does not exist.

To fix it, follow the excellent tips of eferion and mark the definition as static too.

    
answered by 26.10.2018 в 08:54