gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:48:48 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com