未定义的引用错误,我该怎么办?

时间:2018-06-13 07:34:10

标签: c++ undefined-reference dev-c++

您好,这是我第一次来这里,我需要帮助,我必须发送一个最终项目,一个" memorama",但是向我展示了这个问题:

  

main.cpp :(。text + 0xa95):未定义引用`inicio(std :: string *,std :: string *)'

     

main.cpp :(。text + 0x1586):未定义引用`over()'

我不知道为什么,我也说西班牙语所以代码是英语和西班牙语的混合,这是代码:请有人帮助我

Main.cpp的

#include <iostream>
#include <string>
#include <windows.h>
#include <time.h>
#include <unistd.h>
#include "Jugadores.h"
using namespace std;

void inicio (string* j1, string* j2);
void over (void);
int main() 
{
    string materias[]={"", "Calculo", "Programacion", "Ingles", "Fisica", "Español", "Politica", "Deporte", "Matematicas", "Aula", "Fundamentos", "IPOO", "Algebra"};
    string nuevo[24];
    nuevo [0]="0";
    string posicion[]={"","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
    int ale[24]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int numeroa;
    srand (time(NULL));
    string auxiliop, auxilios;
    int i=1,P,S,j=1,p;
    int rep;
    int pares=0;
    int pj1=0,pj2=0,turn=2;
    string d1, d2;
    int ocupados[24];
    int z=0,r=0, yaesta;
    Jugadores jugador1;
    Jugadores jugador2;
    inicio(&d1, &d2);
    jugador1.ingresanombre(d1);
    jugador2.ingresanombre(d2);
    sleep(1000);
    while(i<=24)
    {
        numeroa=1+rand()%24;
        rep=0;
        j=1;
        while(j<=24)
        {
            if (numeroa=ale[j])
            {
                rep=1;
            }
            j++;
        }
        if (rep!=1)
        {
            ale[i]=numeroa;
            i++;
        }
    }
    cout<<"\n\n\n";
    for (i=1;i<=24;i++)
    {
        nuevo[i]=materias[ale[i]];
    }
    cout<<"\n\n";
    i=1;
    while(i<=24)
    {
        cout<<"\t" << posicion[i];
        if (i==6 || i==12 || i==18 || i==24) cout<<"\n\n\n";
        i++;
    }
    i=1;
    while (pares<12)
    {
        cout << "\n\n";
        if (turn%2==0)
        {
            cout << "Es turno de: " << jugador1.mostrarnombre();
        }
        else
        {
            cout << "Es turno de: " << jugador2.mostrarnombre();
        }
        cout << "\nEscoge 2 numeros mostrados en pantalla: \nNumero1: ";
        cin >> P;
        cout << "\nNumero 2: ";
        cin >> S;
        for (r=0;r<=z;r++)
        {
            if (ocupados[r]==P||ocupados[r]==S)
            {
                yaesta=1;
            }
        }
        system ("cls");
        rep=1;
        while (rep == 1)
        {
            if (P==S || P<24 || P<1 || S<24 || S<1 || yaesta==1)
            {
                cout<< "\nError al introducir los datos\nIngresa unos nuevos:\n\n";
                cout<<"Numero 1: ";
                cin >> P;
                cout<< "\nNumero 2: ";
                cin >> S;
                system ("cls");
                yaesta=0;
            }
            else rep=2;
            if (nuevo[P]==nuevo[S])
            {
                ocupados[z]=P;
                ocupados[z+1]=S;
                z+=1;
                pares=pares+1;
                if (turn%2==0)
                {
                    jugador1.entrapuntuacion(1);
                }
                else
                {
                    jugador2.entrapuntuacion(1);
                }
                system ("cls");
                cout<< "\n\n\tCorrecto!!\n\n";
                posicion[P]=nuevo[P];
                posicion[S]=nuevo[S];
                i=1;
                cout<<"\n";
                while (i<=24)
                {
                    cout << "\t" << posicion [i];
                    if (i==6||i==12||i==18||i==24) cout<<"\n\n\n";
                    i++;
                }
            }
            else
            {
                auxiliop=posicion[P];
                auxilios=posicion[S];
                posicion[P]=nuevo[P];
                posicion[S]=nuevo[S];
                i=1;
                cout << "\n";
                while (i <= 24)
                {
                    cout << "\t" << posicion[i];
                    if (i==6||i==12||i==18||i==24) cout << "\n\n\n";
                    i++;
                }
                posicion[P]=auxiliop;
                posicion[S]=auxilios;
                sleep(2000);
                system("cls");
                turn++;
                cout << "\n\nCambio de jugador!!\n\n\n";
                i=1;
                while (i<=24)
                {
                    cout<< "\t" << posicion[i];
                    if (i==6||i==12||i==18||i==24) cout<< "\n\n\n";
                    i++;
                }           
            }
            cout<< "\n los puntos de " << jugador1.mostrarnombre() << " son " << jugador1.mostrarpuntuacion();
            cout<< "\n los puntos de " << jugador2.mostrarnombre() << " son " << jugador2.mostrarpuntuacion() << endl;
        }
        if(jugador1.mostrarpuntuacion() > jugador2.mostrarpuntuacion())
        {
            cout<<jugador1.mostrarnombre() << "\tEs el ganador!!";
        }
        else if (jugador1.mostrarpuntuacion()==jugador2.mostrarpuntuacion())
        {
            cout << "\n\n\t\tTenemos un empate!!";
        }
        else
        {
            cout << jugador2.mostrarnombre() << "\tEs el ganador!!";
        }
        sleep(2000);
        system("cls");
        for (p=0;p<10;p++)
        {
            sleep(500);
            over();
            sleep(500);
            system("cls");
        }
    }
    void over (void);
    {
        cout<<"\t\n•••••  •••••  •     •  •••••  •   •  •• •••   •••••\n";
        cout<<"•      •   •  ••   ••  •      •   •  •       •    •\n";
        cout<<"•  ••  •••••  •  •  •  ••••   •   •  ••••    ••••• \n";
        cout<<"•   •  •   •  •     •  •       • •   •       •  •  \n";
        cout<<"•••••  •   •  •     •  •••••    •    •••••   •   • \n";
    }
    void inicio (string* j1, string* j2);
    {
        cout << "Bienvenido";
    }

    string *j1, *j2;
    sleep(1000);
    cout << "\n\n\n\n\t\t\t\tNombre Jugador 1: ";
    cin>> *j1;
    cout << "\n\n\t\t\t\tNombre Jugador 2: ";
    cin>> *j2;
    system("cls");
    cout<<"\n\n\n\n\n\n\n\t\t\t\t" << *j1 << "\t vs \t" << *j2;
    sleep(1000);
    cout << "\n\n\n\t\t";
    system("pause");
    system("cls");
}

Jugador.h

#ifndef JUGADORES_H
#define JUGADORES_H
#include <string>

using namespace std;
class Jugadores
{
    private:
        int puntos;
        string Nombre;

    public:
        void entrapuntuacion(int);
        int mostrarpuntuacion(void);
        void ingresanombre(string );
        string mostrarnombre(void);
};

#endif

Jugador.cpp

#include "Jugadores.h"
#include <string>
#include <windows.h>
#include <time.h>

using namespace std;

void Jugadores::entrapuntuacion(int P)
{
    if (P==1)
    {
        puntos++;
    }
}

int Jugadores::mostrarpuntuacion()
{
    return(puntos);
}

void Jugadores::ingresanombre(string N)
{
    Nombre=N;
}

string Jugadores::mostrarnombre(void)
{
    return(Nombre);
}

2 个答案:

答案 0 :(得分:2)

  • <string.h>是使用C语言的标头,用于处理const char*字符串。
  • <string>是使用C++语言的标头,用于处理std::string字符串。

所以只需从.h行删除#include

通常,任何C标头都会包含.h,但任何C++标头都不会。请注意,C++标头有C个版本,会略微更改名称。例如,<stdio.h>中的C在“C ++”中变为<cstdio>

因此,在您的代码中,您可能还想更改<time.h>的{​​{1}}。

>适用于<ctime>,因为这是 POSIX标准库, a {{1}标准库。

是的,这令人困惑......

答案 1 :(得分:1)

C ++不允许在另一个函数内定义函数。你很好地隐藏了它,我不得不多次阅读代码以了解会发生什么。

文件顶部是正确的:

using namespace std;

void inicio (string* j1, string* j2);
void over (void);
int main() 
{
    ...

声明 inicioover为函数,定义 main

让我们看一下函数的定义

void over (void);
{
    cout<<"\t\n                      \n";
    cout<<"                                      \n";
    cout<<"                         \n";
    cout<<"                                       \n";
    cout<<"                             \n";
}
void inicio (string* j1, string* j2);
{
    cout << "Bienvenido";
}

string *j1, *j2;
sleep(1000);
cout << "\n\n\n\n\t\t\t\tNombre Jugador 1: ";

这发生在main的定义中。由于;void over(void);末尾的分号(void inicio (string* j1, string* j2);),这些行只是函数的声明。接下来只是一个编译为主要部分的指令块。

应该做什么:只需定义在main结束后的函数:

    ...
    cout << "\n\n\n\t\t";
    system("pause");
    system("cls");
}
void over (void)                   // note the suppression of ;
{
    cout<<"\t\n                      \n";
    cout<<"                                      \n";
    cout<<"                         \n";
    cout<<"                                       \n";
    cout<<"                             \n";
}
void inicio (string* j1, string* j2)
{
    cout << "Bienvenido";
}
相关问题