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

undefined reference error, what should i do?

本文关键字:我该怎么办 错误 引用 未定义      更新时间:2023-10-16

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

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

main.cpp:(.text+0x1586(:对"over(("的未定义引用

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

主.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<<"nnn";
for (i=1;i<=24;i++)
{
nuevo[i]=materias[ale[i]];
}
cout<<"nn";
i=1;
while(i<=24)
{
cout<<"t" << posicion[i];
if (i==6 || i==12 || i==18 || i==24) cout<<"nnn";
i++;
}
i=1;
while (pares<12)
{
cout << "nn";
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 datosnIngresa unos nuevos:nn";
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<< "nntCorrecto!!nn";
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<<"nnn";
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 << "nnn";
i++;
}
posicion[P]=auxiliop;
posicion[S]=auxilios;
sleep(2000);
system("cls");
turn++;
cout << "nnCambio de jugador!!nnn";
i=1;
while (i<=24)
{
cout<< "t" << posicion[i];
if (i==6||i==12||i==18||i==24) cout<< "nnn";
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 << "nnttTenemos 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<<"tn•••••  •••••  •     •  •••••  •   •  •• •••   •••••n";
cout<<"•      •   •  ••   ••  •      •   •  •       •    •n";
cout<<"•  ••  •••••  •  •  •  ••••   •   •  ••••    ••••• n";
cout<<"•   •  •   •  •     •  •       • •   •       •  •  n";
cout<<"•••••  •   •  •     •  •••••    •    •••••   •   • n";
}
void inicio (string* j1, string* j2);
{
cout << "Bienvenido";
}
string *j1, *j2;
sleep(1000);
cout << "nnnnttttNombre Jugador 1: ";
cin>> *j1;
cout << "nnttttNombre Jugador 2: ";
cin>> *j2;
system("cls");
cout<<"nnnnnnntttt" << *j1 << "t vs t" << *j2;
sleep(1000);
cout << "nnntt";
system("pause");
system("cls");
}

朱加多尔·

#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

朱加多尔.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);
}
<string.h>
  • 来自C语言的标头,用于处理const char*字符串。
  • <string>是来自C++语言的标头,用于处理std::string字符串。

因此,只需从#include行中删除该.h即可。

一般来说,任何C标头都会有.h,但任何C++标头都不会。请注意,C标头有C++版本,它们会稍微更改名称。例如,C中的<stdio.h>在"C++"中变为<cstdio>

因此,在您的代码中,您可能还想更改<ctime><time.h>

但这不适用于<unistd.h>,因为这是一个POSIX标准库,而不是一个C标准库。

是的,这令人困惑...

>C++不允许在另一个函数中定义一个函数。你把它隐藏得很好,我不得不多次阅读代码才能理解发生了什么。

文件顶部正确:

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

这将inicioover声明为函数并定义main

让我们看一下函数的定义

void over (void);
{
cout<<"tn                      n";
cout<<"                                      n";
cout<<"                         n";
cout<<"                                       n";
cout<<"                             n";
}
void inicio (string* j1, string* j2);
{
cout << "Bienvenido";
}
string *j1, *j2;
sleep(1000);
cout << "nnnnttttNombre Jugador 1: ";

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

应该做什么:简单地定义main 结束后的函数:

...
cout << "nnntt";
system("pause");
system("cls");
}
void over (void)                   // note the suppression of ;
{
cout<<"tn                      n";
cout<<"                                      n";
cout<<"                         n";
cout<<"                                       n";
cout<<"                             n";
}
void inicio (string* j1, string* j2)
{
cout << "Bienvenido";
}