简体   繁体   English

在C ++中重新定义子类时出错

[英]Error in redefinition of subclass in C++

i get from compiler the following error: 我从编译器得到以下错误:

uni.cpp:63:1: error: redefinition of ‘Studente::Studente()’
 Studente::Studente() : Persona(){
 ^

In file included from uni.cpp:1:0:
uni.h:34:5: note: ‘Studente::Studente()’ previously defined here
     Studente():Persona(){};
     ^

uni.cpp:68:1: error: redefinition of ‘Studente::Studente(std::__cxx11::string, std::__cxx11::string, int, int, int, std::__cxx11::string, std::__cxx11::string)’
 Studente::Studente(string nome_,string cognome_,int anno_,int mese_,int giorno_,string facolta_,string corso_) : Persona(nome_,cognome_,anno_,mese_,gi
 ^

In file included from uni.cpp:1:0:
uni.h:35:5: note: ‘Studente::Studente(std::__cxx11::string, std::__cxx11::string, int, int, int, std::__cxx11::string, std::__cxx11::string)’ previously defined here
     Studente(string nome_,string cognome_,int anno_,int mese_,int giorno_,string facolta_,string corso_) : Persona(nome_,cognome_,anno_,mese_,giorno_)
     ^

And can't find out what's wrong 而且无法找出问题所在

The file are: 该文件是:

UNI.H 统一

#ifndef _UNI_H_
#define _UNI_H_
#include <string>
#include <iostream>
using namespace std;

class Persona{
  private:
    string nome;
    string cognome;
    int anno;
    int mese;
    int giorno;
  public:
    Persona();
    Persona(string nome_,string cognome_,int anno_,int mese_,int giorno_);
    void SetNome(string nome_);
    void SetCognome(string cognome_);
    void SetAnno(int anno_);
    void SetMese(int mese_);
    void SetGiorno(int giorno_);
    string GetNome();
    string GetCognome();
    int GetAnno();
    int GetMese();
    int GetGiorno();
};

class Studente : public Persona{
  private:
    string facolta;
    string corso;
  public:
    Studente():Persona(){};
    Studente(string nome_,string cognome_,int anno_,int mese_,int giorno_,string facolta_,string corso_) : Persona(nome_,cognome_,anno_,mese_,giorno_){};
    void SetFacolta(string facolta_);
    void SetCorso(string corso_);
    string GetFacolta();
    string GetCorso();
};

#endif

UNI.CPP 联合会

#include "uni.h"
#include <iostream>
#include <string>
using namespace std;

Persona::Persona(){
  SetNome("");
  SetCognome("");
  SetAnno(0);
  SetMese(0);
  SetGiorno(0);
}

Persona::Persona(string nome_,string cognome_,int anno_,int mese_,int giorno_){
  SetNome(nome_);
  SetCognome(cognome_);
  SetAnno(anno_);
  SetMese(mese_);
  SetGiorno(giorno_);
}

void Persona::SetNome(string nome_){
  nome=nome_;
}

void Persona::SetCognome(string cognome_){
  cognome=cognome_;
}

void Persona::SetAnno(int anno_){
  anno=anno_;
}

void Persona::SetMese(int mese_){
  mese=mese_;
}

void Persona::SetGiorno(int giorno_){
  giorno=giorno_;
}

string Persona::GetNome(){
  return nome;
}

string Persona::GetCognome(){
  return cognome;
}

int Persona::GetAnno(){
  return anno;
}

int Persona::GetMese(){
  return mese;
}

int Persona::GetGiorno(){
  return giorno;
}


Studente::Studente() : Persona(){
  SetFacolta("");
  SetCorso("");
}

Studente::Studente(string nome_,string cognome_,int anno_,int mese_,int giorno_,string facolta_,string corso_) : Persona(nome_,cognome_,anno_,mese_,giorno_){
  SetFacolta(facolta_);
  SetCorso(corso_);
}

void Studente::SetFacolta(string facolta_){
  facolta=facolta_;
}

void Studente::SetCorso(string corso_){
  corso=corso_;
}

string Studente::GetFacolta(){
  return facolta;
}

string Studente::GetCorso(){
  return corso;
}

I know the main is missing... but that sure is not the problem! 我知道主要的东西不见了...但是那肯定不是问题! It seems that the compiler tries to redefine the classes in the uni.cpp file. 似乎编译器尝试重新定义uni.cpp文件中的类。 Thanks! 谢谢!

Because, you're defining Studente 's Constructor twice. 因为,您两次定义了Studente的构造方法。

In the header: Studente():Persona(){}; 在标题中: Studente():Persona(){}; This should just be Studente(); 这应该只是Studente();

Same with the other constructor. 与其他构造函数相同。

The problem is this constructor definition in the header: 问题是标题中的此构造函数定义:

Studente():Persona(){};

replace it with a declaration, since you have a definition in the .cpp: 用声明代替它,因为您在.cpp中有一个定义:

Studente();

In uni.h in the class Studente you have uni.h中的class Studente您有

Studente():Persona(){};

This declares and defines a default constructor for Studente . 这声明并定义了Studente的默认构造函数。 Then in uni.cpp you have 然后在uni.cpp

Studente::Studente() : Persona(){
  SetFacolta("");
  SetCorso("");
}

Which redefines the default constructor. 重新定义默认构造函数。 You need to remove one of the definitions or change 您需要删除其中一项定义或更改

Studente():Persona(){};

to

Studente();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM