简体   繁体   中英

OOP - Problems with string and member functions in C++

I've already tried to fix every error my program shows, but so far I can't get it to compile and run. Maybe it's because I'm a newbie to OOP. :(

Any suggestions to get my program working? This is my code:

opuntia.h

class Taxonomia {
public:
    string nombre;
    string familia;
    string genero;
    string categoria;
    void introducir();
    void imprimir();
    friend void categoria(Taxonomia&, std::string);
};

opuntia.cpp

#include "opuntia.h"
#include <iostream>

using namespace std;

void Taxonomia::introducir() {
    cout << "Ingresa el nombre de la planta: ";
    cin >> nombre;
    cout << "Ingresa la familia de la planta: ";
    cin >> familia;
    cout << "Ingresa el genero de la planta: ";
    cin >> genero;
}

void Taxonomia::imprimir() {
    cout << "Nombre: " << nombre << endl;
    cout << "Familia: " << familia << endl;
    cout << "Género: " << genero << endl;
}

void categoria(Taxonomia& t, string nueva_categoria) {
    t.categoria = nueva_categoria;
    cout << "Categoria de riesgo de " << t.nombre << " actualizada: " << t.categoria << endl;
}

main.cpp

#include "opuntia.h"
#include <iostream>
#include <string>
#include <locale.h>

using namespace std;

int main() {
    setlocale(LC_ALL, "spanish");

    Taxonomia planta;
    planta.imprimir();

    return 0;
}

This is the list of errors I get:

enter image description here

Problem lies in opuntia.h. In other two files you included "using namespace std;". But forgot to add it in the header. Correct name for string type in C++ is std::string. So correct solution would look like this

#ifndef OPUNTIA_H   // added header guard
#define OPUNTIA_H
#include <iostream>
#include <string>   // added header

class Taxonomia {
public:
    std::string nombre;    // use the fully qualified name, std::string
    std::string familia;
    std::string genero;
    std::string categoria;
    void introducir();
    void imprimir();
    friend void categoria(Taxonomia&, std::string);
};
#endif

Another problem is that you use the wrong operator with cin :

void Taxonomia::introducir() {
    cout << "Ingresa el nombre de la planta: ";
    cin >> nombre;                               // note: >> not <<
    cout << "Ingresa la familia de la planta: ";
    cin >> familia;                              // note: >> not <<
    cout << "Ingresa el genero de la planta: ";
    cin >> genero;                               // note: >> not <<
}

The first line in your .cpp files is:

#include "opuntia.h"

This is the very first thing your C++ compiler reads. So, it goes and reads this file, where it reads:

class Taxonomia {
public:
    string nombre;

At this point your C++ compiler has absolutely no idea, whatsoever, what this is mysterious class called string is all about, what it does, and how it works. And that's what your C++ compiler is telling you.

Later on in main.cpp you do #include <string> , but this is too little too late.

To fix these compilation errors:

  1. Add #include <string> to the beginning of your file.
  2. The <string> header file defines a class called std::string , and not string . You will need to change the declaration in your header files accordingly, because you will do yourself a big, big favor if you completely forget that using namespace std exists in C++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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