简体   繁体   English

OOP - C++ 中的字符串和成员函数问题

[英]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.也许是因为我是 OOP 的新手。 :( :(

Any suggestions to get my program working?有什么建议可以让我的程序正常工作吗? This is my code:这是我的代码:

opuntia.h仙人掌.h

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

opuntia.cpp仙人掌.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.问题在于 opuntia.h。 In other two files you included "using namespace std;".在其他两个文件中,您包含“使用命名空间 std;”。 But forgot to add it in the header.但是忘记在 header 中添加它。 Correct name for string type in C++ is std::string. C++ 中字符串类型的正确名称是 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 :另一个问题是您在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: .cpp文件中的第一行是:

#include "opuntia.h"

This is the very first thing your C++ compiler reads.这是您的 C++ 编译器首先读取的内容。 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.此时,您的 C++ 编译器完全不知道,无论如何,所谓的神秘string是什么,它的作用以及它是如何工作的。 And that's what your C++ compiler is telling you.这就是你的 C++ 编译器告诉你的。

Later on in main.cpp you do #include <string> , but this is too little too late.稍后在main.cpp中执行#include <string> ,但这为时已晚。

To fix these compilation errors:要修复这些编译错误:

  1. Add #include <string> to the beginning of your file.#include <string>添加到文件的开头。
  2. The <string> header file defines a class called std::string , and not string . <string> header 文件定义了一个名为std::string的 class ,而不是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++您将需要相应地更改 header 文件中的声明,因为如果您完全忘记 C++ 中存在using namespace std ,您将为自己带来很大的帮助

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

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