Not sure what is causing error c2600 in code - c++

I have looked at other forum posts and I am still confused. I am very new to coding so a simple answer would be appreciated. I am trying to create a simple program that uses sets and gets to set the player's attributes and then gets them with functions. However, whenever I call the functions I get the error.
Here is my .h file:
#pragma once
#include <iostream>
#include <dos.h>
#include <string>
#include <sstream>
using namespace std;
class Player
{
private:
string name;
int health;
int strength;
int stamina;
int experience;
bool passive;
public:
string GetName();
string SetName(string tName);
int GetHealth();
int SetHealth(int tHealth);
int GetStrength();
int SetStrength(int tStrength);
int GetStamina();
int SetStamina(int tStamina);
int GetExperience();
int SetExperience(int tExperience);
bool GetPassive();
bool SetPassive(bool tPassive);
};
And here is my 1st then 2nd cpp file:
#include <iostream>
#include <iostream>
#include <dos.h>
#include <string>
#include <sstream>
#include "C:\\Users\\Ryan Bell\\Desktop\\School\\2nd Year\\Quarter 1\\Programming\\Week 1\\PlayerClass\\PlayerClass\\PlayerClass.h"
Player::Player()
{
name = "";
health = 100;
strength = 30;
stamina = 100;
experience = 20;
passive = false;
}
string Player::GetName()
{
return name;
}
string Player::SetName(string tName)
{
name = tName;
return "Ok";
}
int Player::GetHealth()
{
return health;
}
int Player::SetHealth(int tHealth)
{
health = tHealth;
}
int Player::GetStrength()
{
return strength;
}
int Player::SetStrength(int tStrength)
{
strength = tStrength;
}
int Player::GetStamina()
{
return stamina;
}
int Player::SetStamina(int tStamina)
{
stamina = tStamina;
}
int Player::GetExperience()
{
return experience;
}
int Player::SetExperience(int tExperience)
{
experience = tExperience;
}
bool Player::GetPassive()
{
return passive;
}
bool Player::SetPassive(bool tPassive)
{
passive = tPassive;
}
#include <iostream>
#include <iostream>
#include <dos.h>
#include <string>
#include <sstream>
#include "C:\\Users\\Ryan Bell\\Desktop\\School\\2nd Year\\Quarter 1\\Programming\\Week 1\\PlayerClass\\PlayerClass\\PlayerClass.h"
int main()
{
Player Player1;
Player1.SetName("Jake");
Player1.SetHealth(100);
Player1.SetStrength(30);
Player1.SetStamina(50);
Player1.SetExperience(0);
Player1.SetPassive(true);
cout << "Player " << Player1.GetName() << ".";
}
Thank you for your time and help!

I don't see a Player Constructor declared:
// I see the Player Constructor definition here.
Player::Player()
{
.....
But this method is not declared in the class.
class Player
{
private:
.....
public:
Player(); // Add this line.
string GetName();
PS. Your code should compile and "probably" runs but is not good. If you can show it working, you can take it to Code Review and get them to give you advice on how to make it better (and some obvious errors corrected).

The compilation of your code gives:
2.cpp: In member function ‘int Player::SetStamina(int)’:
2.cpp:18:1: warning: no return statement in function returning non-void [-Wreturn-type]
18 | }
| ^
2.cpp: In member function ‘int Player::SetExperience(int)’:
2.cpp:28:1: warning: no return statement in function returning non-void [-Wreturn-type]
28 | }
| ^
2.cpp: In member function ‘bool Player::SetPassive(bool)’:
2.cpp:38:1: warning: no return statement in function returning non-void [-Wreturn-type]
38 | }
These can be fixed by turning all setters as functions returning void, e.g:
void Player::SetStamina(int tStamina)
Do it both in *.h and player.cpp.
After fixing this, we bump onto linker errors:
/usr/bin/ld: /tmp/ccQk0cvm.o: in function `main':
main.cpp:(.text+0x6c): undefined reference to `Player::SetName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: main.cpp:(.text+0xa7): undefined reference to `Player::SetHealth(int)'
/usr/bin/ld: main.cpp:(.text+0xb8): undefined reference to `Player::SetStrength(int)'
/usr/bin/ld: main.cpp:(.text+0x11a): undefined reference to `Player::GetName[abi:cxx11]()'
collect2: error: ld returned 1 exit status
This means that you forgot to define Player::GetName, Player::SetStrength(int), and 2 other methods.
This leads us to the simplest solution (for now): modify the public section of your class so that it implements all functions:
public:
std::string GetName() const { return name; }
void SetName(string tName) { name = tName; }
int GetHealth() const { return health; }
void SetHealth(int tHealth) { health = tHealth; }
int GetStrength() const { return strength; }
void SetStrength(int tStrength) { strength = tStrength; }
int GetStamina() const { return stamina; }
void SetStamina(int tStamina) { stamina = tStamina; }
int GetExperience() const { return experience; }
void SetExperience(int tExperience) { experience = tExperience; }
bool GetPassive() const { return passive; }
void SetPassive(bool tPassive) { passive = tPassive; }
and don't use a separate source file for the class member definitions.
Alternatively, you can keep the file, but then you must add all missing function definitions in it.
Notice that I declared all getters as const member functions. Believe me, this is how they should be defined.

Related

C++ Object declaration needs another object as parameter

Object-oriented C++ here.
I'm supposed to code a Microwave object that "heats" a FrozenMeal object.
One method of the Microwave object, called void heatMeal(FrozenMeal), is supposed to take an instance of a FrozenMeal object as a parameter and increase its temperature.
FrozenMeal.h
#include <string>
class FrozenMeal {
public:
FrozenMeal(std::string, int);
void setTemperature(double);
std::string getName() const;
int getVolume() const;
double getCoeffizient() const;
double getTemperature() const;
private:
std::string name;
int volume;
double temperature;
double coeffizient;
};
FrozenMeal.cpp
#include <string>
#include "FrozenMeal.h"
using namespace std;
FrozenMeal::FrozenMeal(string mealName, int mealVolu) {
name = mealName;
volume = mealVolu;
temperature = -18;
coeffizient = 0.24;
}
void FrozenMeal::setTemperature(double mealTemp) { temperature = mealTemp; }
string FrozenMeal::getName() const { return name; }
int FrozenMeal::getVolume() const { return volume; }
double FrozenMeal::getCoeffizient() const { return coeffizient; }
double FrozenMeal::getTemperature() const { return temperature; }
Microwave.h
#include "FrozenMeal.h"
class Microwave {
public:
Microwave();
void morePower();
void lessPower();
void setPeriod(double);
void heatMeal(FrozenMeal); // <----------------------------
int getPower() const;
double getPeriod() const;
private:
int power;
double period;
};
Microwave.cpp
#include "Microwave.h"
using namespace std;
Microwave::Microwave() {}
void Microwave::morePower() { if (power < 1000) power += 200; }
void Microwave::lessPower() { if (power > 200) power -= 200; }
void Microwave::setPeriod(double sessionPeri) { period = sessionPeri; }
void Microwave::heatMeal(FrozenMeal mealInst) {
mealInst.setTemperature(80); //example
}
int Microwave::getPower() const { return power; }
double Microwave::getPeriod() const { return period; }
Now, my problem is that my compiler says that the file FrozenMeal.h apparently redefines the object type of FrozenMeal, even though that should be the job of the FrozenMeal.cpp file, and compiling is unsuccessful.
I tried including FrozenMeal.h to Microwave.cpp but that resulted in even more compiler errors.
I feel like I'm doing something horribly wrong here.
Add include guards to your header files so its contents doesn't get included more than once:
FrozenMeal.h:
#ifndef FROZENMEAL_H_INCLUDED
#define FROZENMEAL_H_INCLUDED
// your code ...
#endif /* FROZENMEAL_H_INCLUDED */
Microwave.h:
#ifndef MICROWAVE_H_INCLUDED
#define MICROWAVE_H_INCLUDED
// your code ...
#endif /* MICROWAVE_H_INCLUDED */
Also, you never initialize int Microwave::power and double Microwave::period so you will read and write garbage values in Microwave::morePower() and Microwave::lessPower()
As suggested in the comments, you want to take the parameter of Microwave::heatMeal() by reference so the function can modify the passed object:
void Microwave::heatMeal(FrozenMeal &mealInst)
// ^

error:function definition does not declare parameters in setter

I wrote a simple c++ program and it gave me error in the setter, function definition does not declare parameters, although there was a setter above it and it worked correctly the error is in the function named setattack here is the code:
{
#include <iostream>
using namespace std;
class Warrior{
private:
string name;
int attack;
int defense;
public:
void setname(string m){
name=m;
}
string getname(){
return name;}
void setattack{int aw}{
attack=aw;
}
int getattack(){
return attack;}
void setdefense{int dw}{
defenset=dw;
}
int getdefense(){
return defense;}
void kill(const Monster &monster){
}
};
int main()
{
return 0;
}
}
In your set function you have used {} curly braces for arguments I think it should be (), and there is an extra } after main.

No default constructor exists for class c++ [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
Hello,
I'm trying to instantiate an anonymous object with a std::string variable 'name'. But intellisenen gives me error saying
E0291 no default constructor exists for class "Player" GoldGame e:\C++ Projects\Hello World\GoldGame\GoldGame.cpp 17
I have provided a constructor which can just take a std::string variable since other parameters are provided with default value.
Can you guys shed some light on this?
What confuses me even more is that when I change
Player(name);
to
Player a(name);
or to
Player("test");
then intellisense becomes totally fine with those.
GoldGame.cpp
#include "stdafx.h"
#include "Creature.h"
#include "Player.h"
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter your name: ";
std::string name;
std::cin >> name;
Player(name);
return 0;
}
Creature.h
#pragma once
#include <string>
class Creature
{
public:
Creature(const std::string &name, const char symbol, const int health, const int damage, const int gold);
~Creature();
//getters
const std::string& getName() { return m_name; }
const char getSymbol() { return m_symbol; }
const int getHealth() { return m_health; }
const int getDamage() { return m_damage; }
const int getGold() { return m_gold; }
//health, gold and dead
void reduceHealth(const int healthMinus);
void addGold(const int gold);
bool isDead();
private:
std::string m_name;
char m_symbol;
int m_health;
int m_damage;
int m_gold;
};
Creature.cpp
#include "stdafx.h"
#include "Creature.h"
Creature::Creature(const std::string & name, const char symbol, const int health, const int damage, const int gold)
:m_name(name), m_symbol(symbol), m_health(health), m_damage(damage), m_gold(gold)
{
}
Creature::~Creature()
{
}
void Creature::reduceHealth(const int healthMinus)
{
m_health -= healthMinus;
}
void Creature::addGold(const int gold)
{
m_gold += gold;
}
bool Creature::isDead()
{
if (m_health>0)
{
return true;
}
else
{
return false;
}
}
Player.h
#pragma once
#include "Creature.h"
#include <string>
class Player :
public Creature
{
public:
Player(const std::string &name, const char symbol='#', const int health=10, const int damage=1, const int gold=0);
~Player();
const int getLevel() { return m_level; }
void levelUp();
bool hasWon();
private:
int m_level;
};
Player.cpp
#include "stdafx.h"
#include "Player.h"
Player::Player(const std::string & name, const char symbol, const int health, const int damage, const int gold)
:Creature(name,symbol,health,damage,gold)
{
}
Player::~Player()
{
}
void Player::levelUp()
{
++m_level;
}
bool Player::hasWon()
{
if (m_level>=20)
{
return true;
}
else
{
return false;
}
}
Player(name); does not do what you think it does. It declares a new variable name of type Player and calls a default constructor. If you want to instantiate an anonymous Player variable then you need to write
(Player(name));
// or
Player{name}; // list initialization since C++11

undefined reference to my constructor

I have a simple class which I cannot instantiate and I don't know why...
Please help me !
-------Test.cpp-------
#include<iostream>
using namespace std;
#include "meteo.h"
int main()
{
Meteo meteo;
}
-------meteo.h---------
#ifndef METEO_H
#define METEO_H
class Meteo
{
public:
Meteo();
int Get(int i);
private:
char *list[];
};
#endif
-------meteo.cpp--------
#include "meteo.h"
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
int Meteo::Get(int i)
{
return list[i];
}
I get the error: "undefined reference to `Meteo::Meteo()'"
It seems that the problem is that the compiler issued an error when was compiling the constructor
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
and did not generate the object module.
This record
list[]("Sec","Venteux","Humide");
is invalid.
Try to change the class definition like
class Meteo
{
public:
Meteo();
int Get(int i);
private:
const char *list[3];
};
and define the constructor like
Meteo::Meteo() : list { "Sec","Venteux","Humide" }
{
}
The other reason might be that you did not include object module meteo in the project.
Take into account that this member function
int Meteo::Get(int i)
{
return list[i];
}
is also wrong. The type of elements of the array is const char * not int.

Undefined reference to function in class

main.cpp:
#include <iostream>
#include "pokemonList.h"
void pokemonLookup();
int main() {
pokemonLookup();
return 0;
}
void pokemonLookup() {
pokemonList pL;
std::cout<<std::endl<<"What Pokemon do you want to look up? ";
std::string pokemonLookup;
std::cin>>pokemonLookup;
pL.displayPokemon(pokemonLookup);
}
pokemonList.h:
#ifndef POKEMONLIST_H
#define POKEMONLIST_H
#include <iostream>
class pokemonList
{
private:
struct pokemonTemplate {
std::string pokemonName;
std::string pokemonMoves[3];
int pokemonLevel;
int baseATK;
int baseDEF;
int baseSPATK;
int baseSPDEF;
int baseSPEED;
};
pokemonTemplate bulbasaur;
pokemonTemplate pikachu;
public:
void displayPokemon(std::string pokemon);
protected:
};
#endif // POKEMONLIST_H
pokemonList.cpp:
#include "pokemonList.h"
/*
pokemonTemplate* bulbasaur() {
bulbasaur.pokemonName = "Bulbasaur";
bulbasaur.pokemonMoves[3];
bulbasaur.pokemonLevel = 5;
bulbasaur.baseATK = 10;
bulbasaur.baseDEF = 10;
bulbasaur.baseSPATK = 10;
bulbasaur.baseSPDEF = 10;
bulbasaur.baseSPEED = 10;
}
pokemonTemplate* pikachu() {
pikachu.pokemonName = "Pikachu";
pikachu.pokemonMoves[3];
pikachu.pokemonLevel = 5;
pikachu.baseATK = 8;
pikachu.baseDEF = 10;
pikachu.baseSPATK = 12;
pikachu.baseSPDEF = 6;
pikachu.baseSPEED = 15;
}
*/
void displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
I realize in the pokemonList.cpp file I have a bunch of Bad Code commented out, that isn't what this question is about. When I try to compile, I get a single error in the main.cpp saying:
D:/CodeBlocks/Projects/RelearningCPlusPlus/main.cpp:15: undefined reference to `pokemonList::displayPokemon(std::string)'
You defined a function displayPokemon() but it isn't defined as a member function. To define it as a member function outside the class definition, you need to mention the class name:
void pokemonList::displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
it should be
void pokemonList::displayPokemon(std::string pokemon){....
You need to change this:
void displayPokemon(std::string pokemon) {
to this:
void pokemonList::displayPokemon(std::string pokemon) {
Dietmar's answer solves the problem.
But I want to add that it is better to passing argument by reference instead of passing by value with std::string pokemon in this case, thus reducing overhead. So the displayPokemon function should be:
void pokemonList:displayPokemon(std::string &pokemon) const {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
};
Also, remember to change the corresponding member function declaration.
displayPokemon function is declared as a pokemonList member function but is never defined.
This:
void displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
is a definition of some other function (with the same name) which is not a pokemonList class member function. To tell the compiler that this is a class pokemonList member function you must preceed its name with pokemonList::
void pokemonList::displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
Alternatively, you can define displayPokemon inside pokemonList class:
class pokemonList
{
private:
struct pokemonTemplate {
std::string pokemonName;
std::string pokemonMoves[3];
int pokemonLevel;
int baseATK;
int baseDEF;
int baseSPATK;
int baseSPDEF;
int baseSPEED;
};
pokemonTemplate bulbasaur;
pokemonTemplate pikachu;
public:
void displayPokemon(std::string pokemon) {
std::cout<<std::endl<<"Looking up: " + pokemon<<std::endl;
}
protected:
};
The difference is that in the last case the displayPokemon will be an inline function.