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:
CodePudding user response:
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 <<
}
CodePudding user response:
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:
- Add
#include <string>
to the beginning of your file. - The
<string>
header file defines a class calledstd::string
, and notstring
. 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 thatusing namespace std
exists in C