Understanding the ambiguating new declaration of function error - c++

I don't understand this error
here is a link to view the code online:
https://onlinegdb.com/rkirYvU_M
I am trying to add the names of the drivers, owners, and model to vectors, and we need to use pointers and files.
Here is my main file:
#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;
}
Here is the person.h file:
#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
Here is the person.c++ file:
#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;
}
Here is the car.h file:
#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();
^
You seem to be confusing function declarations with member functions. Just declare a Person object on the stack and call the method through it's object. Do the same for your Car object. You can use your objects like this.
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;
You will have to store your temporary objects before they go out of scope if you want to use them later.

Related

How to get the name of an Object

I am coding a little RPG(Role Playing Game)
Here is the situation: I created an object Personnage.
In my classes, I created a method atttaquer. But I would like that after calling my method attaquer it writes something like this: Goliath attaque David . But to that, I need to grab the name of the Object. Because the player may want to edit the name of Object (The personage name) before playing.
There is my code:
Personnage.h
#ifndef Personnage_h
#define Personnage_h
#include <string>
#include "Arme.h"
class Personnage{
//methods
public:
Personnage();
Personnage(std::string nomArme, int degatsArme);
Personnage(int vie, int mana);
// ~Personnage();
void recevoirDegats(int nbDegats);
void attaquer(Personnage &cible);
private:
// Attributs
int m_vie;
int m_magie;
std::string m_nom;
};
#endif
My Personnage.cpp code:
#include "Personnage.h"
#include <string>
#include <iostream>
void Personnage::recevoirDegats(int nbDegats){
m_vie -= nbDegats;
if (m_vie < 0) {
m_vie = 0;
}
}
void Personnage::attaquer(Personnage &cible){
cible.recevoirDegats(m_arme.getDegats());
// if David attacks Goliath I want to write std::cout << David << "attaque "<< Goliath << endl; but I do not know how to grab the name of the object after it's creation
}
There is my main.cpp
#include <iostream>
#include <string>
#include "Personnage.h"
//#include "Personnage.cpp"
#include <ctime>
using namespace std;
int main()
{
Personnage David, Goliath, Atangana("Ak47", 35);
Goliath.attaquer(David);
return 0;
}
If you want to give your objects names, it cannot be the variable names. They are only meant for the compiler and they are fixed. So you need to create a class that can have a name:
class NamedObject
{
private:
std::string m_name;
public:
const std::string& getName() const
{
return m_name;
}
void setName(const std::string& name)
{
m_name = name;
}
}
And if you want your classes to have a name, the easiest way would be to derive from it:
class Personnage : NamedObject {
Then you can say:
Personnage player1, player2;
player1.setName("David");
player2.setName("Goliath");
Alternatively, you can get those string from user input.
And if you need to address one by name:
std::cout << player1.getName() << " please make your move." << std::endl;

Can't set member data when nested in a class

I'm having this issue on another program, but I tried to simplify it with this one. I cannot set the weapon name through p.getWeaopn().setName("sword"); It works fine when I simply set it through its own object, but when I try to access the setter through player it doesn't set anything.
#include <iostream>
#include <string>
#include "Player.h"
#include "Weapon.h"
using namespace std;
int main()
{
Player p; // Player contains only a Weapon weapon;
Weapon w; // Weapon only contains a string name;
//w.setName("sword"); // this changes the name of the weapon
p.setWeapon(w);
p.weapon.setName("sword"); // this also changes the name
p.getWeapon().setName("sword"); // this is not setting the name. Why?
// checking if weapon has a name
if (p.getWeapon().getName().empty())
{
cout << "Weapon name is empty!" << endl;
}
else
{
cout << "Weapon name is " << p.getWeapon().getName() << endl;
}
}
Weapon.h
#pragma once
#include <string>
using namespace std;
class Weapon
{
private:
string name;
public:
string getName();
void setName(string);
};
Weapon.cpp
#include "Weapon.h"
string Weapon::getName()
{
return name;
}
void Weapon::setName(string n)
{
name = n;
}
Player.h
#pragma once
#include "Weapon.h"
class Player
{
private:
Weapon weapon;
public:
Weapon getWeapon();
void setWeapon(Weapon);
};
Player.cpp
#include "Player.h"
Weapon Player::getWeapon()
{
return weapon;
}
void Player::setWeapon(Weapon w)
{
weapon = w;
}
Weapon Player::getWeapon()
You return a copy and not a reference of the weapon, so any change to the copy does not affect the original.
For return a reference, use & operator:
Weapon& Player::getWeapon()
{
return this->weapon;
}
Player::getWeapon() returns a copy of the weapon every time instead of a reference to the weapon. Changing the name in the copy changes nothing in the original.

prototype for "...." does not match any in class "..."

I started to pick up some c++ programming and i am learning the concept of classes and objects. So I looked on the web for some exercises I can practice on. I have read that it is good practice to have the main, header and constructor file separated and not into one long file.
I'm trying to break the following code into three separate files :
// Exercises: Classes
// Exercise 3
// Exercises: Classes
// Exercise 3
#include <iostream>
using namespace std;
class Student{
public:
char *name;
int mark1;int mark2;
Student(char* na, int ma1,int ma2){
name=na;mark1=ma1;mark2=ma2;
}
int calc_media(){return (mark1+mark2)/2;}
void disp(){
cout << "Student:" << name << " \n media:"<< calc_media() <<"\n";
}
};
int main(){
char* nam;int m1,m2;
cout << "Enter name:";
cin>> nam;
cout << "Enter marks of two subjects:";
cin>> m1;
cin>> m2;
Student student1(nam,m1,m2);
student1.disp();
return 0;
}
into the following files:
main.cpp:
#include <iostream>
#include <string>
#include "student_example.h"
using namespace std;
int main()
{
int marc1,marc2;
char nam;
cout<<"Please enter the name of the student: ";
cin>>nam;
cout<<"Please enter the two grades of the student"<<"\n grade one:";
cin>>marc1;
cout<<"Grade two";
cin>>marc2;
student_Example student1;
student1.disp();
return 0;
}
header file (student_Example.h)
#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>
class student_Example
{
public:
char name;
int mark1, mark2;
int calc_media(){
return (mark1+mark2/2);
}
void disp(){
std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
}
};
And constructor:
#include <iostream>
#include <string>
#include "student_Example.h"
student_Example::student_Example(char nam, int marc1, int marc2)
{
name=nam;
mark1=marc1;
mark2=maec2;
}
Im getting the error
"error: prototype for **'student_Example::student_Example(char, int, int)' does not match any class 'student_Example'**
Any advice what may be going on here? Thanks in advance :)
Your header file with class student_Example doesn't promise a constructor. (And seems to be missing and #endif)
#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>
class student_Example
{
public:
student_Example(char nam, int marc1, int marc2); //<-- as pointed out in the error
char name;
int mark1, mark2;
int calc_media(){
return (mark1+mark2/2);
}
void disp(){
std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
}
};
#endif //<-- this too
While we are there we can use an member initialiser list in the constructor
student_Example::student_Example(char nam, int marc1, int marc2) :
name(nam),
mark1(marc1),
mark2(marc2) //assume maerc2 was a typo
{
}
Edit:
Note that student_Example(char nam, int marc1, int marc2) is a declaration that you will define a constructor taking a char and two ints., which you have done in your cpp file.
You can make an object like this
student_Example example('n', 1, 2);
Without this non-default constructor, a default constructor taking no parameters would have been automatically generator for you, so you could have made an object like this:
student_Example example;
Now you have defined a constructor that will no longer happen. You either need to add this to your class, or make sure you use the constructor taking parameters.
doctorlove fixes your issue, but is also good practice to have the methods in the .cpp file, like this:
student_Example.h
#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>
class student_Example
{
public:
student_Example(char nam, int marc1, int marc2); //<-- as pointed out in the error
char name;
int mark1, mark2;
int calc_media();
void disp();
};
#endif
student_Example.cpp
#include "student_Example.h"
student_Example::student_Example(char nam, int marc1, int marc2) :
name(nam),
mark1(marc1),
mark2(marc2) //assume maerc2 was a typo
{
}
int student_Example::calc_media(){
return (mark1+mark2/2);
}
void student_Example::disp(){
std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
}

I dont understand whats wrong mith my code with multiple classes (I started c++ multiple classes recently)

I recently started learning and working on multiple classes but i can't understand why my code doesn't work.
I get these errors:
errors photo
This is my code separated in these classes
main.cpp:
#include <iostream>
#include <string>
#include "dalykai.h"
using namespace std;
int main(){
string input;
dalykai dalykaiObj;
cout << "Type some name: "; cin >> input;
dalykaiObj.setName(input);
cout << "Jusu ivestas vardas yra: " << dalykaiObj.getName() << endl;
return 0;
}
dalykai.h
#ifndef DALYKAI_H
#define DALYKAI_H
using namespace std;
class dalykai{
public:
dalykai();
void setName(string x);
string getName();
protected:
private:
string name;
};
#endif // DALYKAI_H
dalykai.cpp
#include <iostream>
#include <string>
#include "dalykai.h"
using namespace std;
string name;
dalykai::dalykai(){
cout << "Object was created successfully!\n";
}
dalykai::void setName(string x){
name = x;
}
dalykai::string getName(){
return name;
}
This is wrong:
dalykai::void setName(string x){
name = x;
}
It should be:
void dalykai::setName(string x){
name = x;
}
In addition. Avoid using using namespace std in headers. Your get function should end with const since it doesn't change class member variables.

C++ Class object as argument in other class function

I have a C++ project in Visual Studio 2015.
GameManager.h and Input.h both give me a syntax error: identifier 'Player'. This happens because I want to give an object of type 'Player' as an argument to functions in these two Header files and their appropriate C++ Files.
How do I fix that? For further information I have provided my code.
main.cpp:
#include "GameManager.h"
#include "Input.h"
#include "Player"
#include <iostream>
#include <string>
using namespace std;
const int maxPlayerCnt = 10;
static Player p1, p2, morePlayers[maxPlayerCnt];
int main()
{
GameManager game;
game.Game(p1, p2, morePlayers);
return 0;
}
It creates an object of type GameManager and 3 objects of type Player.
GameManager.h:
#include "Player.h"
class GameManager
{
public:
void Game(Player p1, Player p2, Player morePlayers[]);
};
GameManager.cpp:
#include "GameManager.h"
void GameManager::Game(Player p1, Player p2, Player morePlayers[])
{
int playerCnt = 0;
Input input;
input.getPlayerDetails(playerCnt, p2);
input.getMorePlayerDetails(playerCnt, morePlayers);
}
It creates an object of type Input to use further functions and will get more code, once I figure this problem out. And then calls to functions with specific arguments it gets from main.cpp
Input.h:
#pragma once
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
class Input
{
public:
Input();
void getPlayerDetails(int &playerNum, Player p);
void getMorePlayerDetails(int &playerNum, Player p[]);
};
It includes everything Input.cpp needs and initializes the funcitons
Input.cpp:
#include "Input.h"
void Input::getPlayerDetails(int &playerNum, Player p)
{
playerNum++;
string currentName;
char currentSymbol;
cout << "Player " << playerNum << ", what is your name?\n";
cin >> currentName;
p.setName(currentName);
cout << currentName << " what is your symbol?\n";
cin >> currentSymbol;
p.setSymbol(currentSymbol);
}
void Input::getMorePlayerDetails(int &playerNum, Player p[])
{
int plNum = playerNum;
if (playerNum >= 12)
cout << "You can't get another player!\n";
else
{
//getPlayerDetails(p[playerNum - 2], (plNum - 2));
}
}
It for now has all the functions needed and both get an object of type Player. And the second function is not quite done now. But that is not important.
Player.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player
{
private:
string _name;
char _symbol;
public:
Player();
void getName();
void setName(string name);
void setSymbol(char symbol);
};
Player.cpp:
#include "Player.h"
Player::Player()
{
}
void Player::getName()
{
cout << "I have no name!\n";
}
void Player::setName(string name)
{
_name = name;
}
void Player::setSymbol(char symbol)
{
_symbol = symbol;
}
If you can help me, I would be pleased to see your response.