了解函数错误的歧义新声明
Understanding the ambiguating new declaration of function error
我不明白这个错误以下是在线查看代码的链接:
https://onlinegdb.com/rkirYvU_M
我正在尝试将驱动程序、所有者和模型的名称添加到向量中,我们需要使用指针和文件。
这是我的主要文件:
#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
我正在尝试理解此错误。
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;
如果要稍后使用临时对象,则必须在临时对象超出范围之前存储它们。
相关文章:
- 如何从构造函数声明新的私有变量?
- 使用 basic_string 声明新类型的字符串
- STD :: bad_alloc在声明新的int [n]时
- 了解函数错误的歧义新声明
- 使用模板在类中声明新函数
- 使用括号会在声明新节点时会产生错误
- QT应用程序声明新对象后崩溃
- 新声明在使用delete时包含垃圾值和堆损坏
- 是否有C#中的C 新声明
- 当我想为结构体声明新成员时,有些麻烦我不明白
- 声明新节点但不等于 NULL
- 使用以前的函数参数声明新的函数参数是否合法
- 如何在Qt 5中声明新信号槽语法作为函数参数
- 获取错误:新声明符中的表达式必须具有整型或枚举类型C++
- 在初始化中使用新声明的变量(int x=x+1)
- 如何解决编译器枚举重新声明冲突
- 非ptr新声明符中的错误表达式
- 如何使用c++中的类模板声明新的运算符
- 用C++中现有映射的地址分配新声明的映射的地址可以吗
- 正在声明新的数字类型