c++ - 理解函数错误的模糊新声明

标签 c++

我不明白这个错误 这是在线查看代码的链接:

https://onlinegdb.com/rkirYvU_M

我正在尝试将驱动程序、所有者和模型的名称添加到 vector 中,我们需要使用指针和文件。

这是我的主文件:

#include "person.h"
#include "car.h"
#include <iostream>
#include <vector> 

std::vector <Person*>people;
std::vector <Car*> cars;

int main()
{
    bool done = false;

    Person person;

    while(! done)
    {
        std::cout << "\n Please enter the owners ";
        Person*prompt_info();
        std::cout << "\n Please enter the drivers ";
        Car*prompt_info();
        Car*set();
        Car*print();

    }
    return 0;
}

这是 person.h 文件:

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


class Person
{

public:
    Person();
    std::string get_name();
    int get_age();
    void prompt_info();

private:
    std::string name;
    int age;


};

#endif 

这是 person.c++ 文件:

#include "person.h"

Person::Person()
{

}


void Person::prompt_info()
{

    std::cout << " name: ";
    std::cin >> name;

    std::cout << "enter their age: ";
    std::cin >> age;

}

std::string Person::get_name()
{

    return name;

}


int Person::get_age()
{
    return age;

}

这是 car.h 文件:

#ifndef CAR_H
#define CAR_H
#include <string>
#include <iostream>
#include "person.h"    
using namespace std;
class Car
{    
public:
    Car();
    std::string get_model();
    Person* get_owner();
    Person* get_driver();
    void print();
    void set(Person _owner,Person get_driver);
    void prompt_info();

private:
    std::string model;
    Person*  owner;
    Person* driver;
};
#endif 

最佳答案

I am trying to understand this error.

main.cpp:23:25: error: ambiguating new declaration of 'Car* prompt_info()'
     Car*prompt_info();
                     ^

您似乎将函数声明与成员函数混淆了。只需在堆栈上声明一个 Person 对象并通过它的对象调用该方法即可。对您的 Car 对象执行相同的操作。您可以像这样使用您的对象。

while(! done)
{
    Person person;  ///< Person object named 'person'
    Car car;        ///< Car object named 'car'
    std::cout << "\n Please enter the owners ";
    person.prompt_info();
    std::cout << "\n Please enter the drivers ";
    car.prompt_info();
    car.set();
    car.print();
    // TODO do something with your objects (store to vector?)
    // next time through the loop your person and car will
    // get initialized all over again
}
return 0;

如果您想稍后使用临时对象,则必须在它们超出范围之前存储它们。

关于c++ - 理解函数错误的模糊新声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49063919/

相关文章:

c++ - 需要帮助重组和/或重构这个关于多个类之间继承的 c++ 代码

c++ - 通用迭代器在不使用 C++ 模板的情况下访问 vector 的元素

c++ - 在 Eclipse 中使用 C++

c++ - 调用隐藏基类方法的最佳方式

c++ - vtkResliceImageViewer 显示不正确(太暗)的 dicom 图像

c++ - 当 std::uint16_t 在 union 明确定义的行为中处于事件状态时,是否正在访问 std::array<std::uint8_t, 2> ?

c++ - Opencv 在 Linux 上没有检测到火线网络摄像头

java - OpenCV(JavaCV)与 OpenCV(C/C++ 接口(interface))

C++ shared_ptr use_count for nullptr

c++ - 这些数据类型之间有什么区别?