This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
My code is raising this error:
supportCrew.cpp:(.text+0x15): undefined reference to `sportsPsychologist::sportsPsychologist()'
supportCrew.cpp:(.text+0x25): undefined reference to `Physiotherapist::Physiotherapist()'
supportCrew.cpp:(.text+0x35): undefined reference to `trainer::trainer()'
supportCrew.cpp:(.text+0x47): undefined reference to `trainer::trainer()'
The two relevent classes/cpp files are supportCrew and Person
Person.cpp
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <chrono>
#include <random>
#include <algorithm>
Person::Person(std::string pName, int pAge, int pExperience) {
name = pName;
age = pAge;
experience = pExperience; //years of experience
};
Athlete::Athlete(std::string name, int age, int experience, std::string gender, double height, double weight)
{
gender = gender;
height = height;
weight = weight;
};
Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness) {
readinessScore = readiness;
recoveryScore = recovery;
};
sportsPsychologist::sportsPsychologist(std::string name, int age, int experience, int pressure, int injury, int successFail) {
pressurescore = pressure;
injuryscore = injury;
successfailscore = successFail;
};
trainer::trainer(std::string name, int age, int experience,std::string specialization, int performance, int consistency) {
specialization = specialization;
performanceScore = performance;
consistencyScore = consistency;
};
Person.h
#ifndef PERSON
#define PERSON
class Person {
std::string name;
int experience; //in years
int age;
public:
Person(std::string pName, int pAge, int pExperience);
Person();
virtual ~Person(){};
std::string getName() {return randomName();};
int getAge(){return randomAge();};
int getExperience(){return randomExperience();};
void printData(std::string, int, int);
std::string randomName();
int randomAge();
int randomExperience();
};
class Athlete: public Person{
private:
std::string gender;
double height;
double weight;
public:
Athlete(std::string pName, int pAge, int pExperience, std::string gender, double height, double weight);
Athlete();
virtual ~Athlete(){};
std::string &getGender(){return gender;};
std:: string randomGender();
double randomHeight();
double randomWeight();
};
class Physiotherapist: public Person{
private:
int recoveryScore;
int readinessScore;
public:
Physiotherapist(std::string pName, int pAge, int pExperience, int recoveryScore, int readinessScore);
Physiotherapist();
int &getscoreRecovery(){return recoveryScore;};
int &getscoreReadiness(){return readinessScore;};
};
class sportsPsychologist: public Person {
private:
int pressurescore;
int injuryscore;
int successfailscore;
public:
sportsPsychologist(std::string pName, int pAge, int pExperience, int pressureScore, int injuryScore, int successfailScore);
sportsPsychologist();
int &getscorePressure(){return pressurescore;};
int &getscoreInjury(){return injuryscore;};
int &getscoreSuccues_Fail(){return successfailscore;};
};
class teamManager: public Person {
public:
teamManager(std::string pName, int pAge, int pExperience);
teamManager();
};
class trainer: public Person {
private:
std::string specialization;
int performanceScore;
int consistencyScore;
public:
trainer(std::string pName, int pAge, int pExperience, std::string specialization, int performanceScore, int cocsistencyScore);
trainer();
std::string &getSpecialization(){return specialization;};
int &getscorePeformance(){return performanceScore;};
int &getscoreConsistency(){return consistencyScore;};
};
#endif
supportCrew.h
#ifndef SUPPORTCREW
#define SUPPORTCREW
#include "Person.h"
class supportCrew {
private:
sportsPsychologist Psychologist;
Physiotherapist physio;
trainer trainer1;
trainer trainer2;
public:
supportCrew(sportsPsychologist,Physiotherapist,trainer,trainer);
supportCrew();
};
#endif
supportCrew.cpp
#include <iostream>
#include <sstream>
#include <fstream>
#include "compDay.h"
#include "Competion.h"
#include "Events.h"
#include "Location.h"
#include "Octathlon.h"
#include "Person.h"
#include "supportCrew.h"
#include "theTeam.h"
#include "weatherSystem.h"
supportCrew::supportCrew() {
}
It seem the skill you are missing is how to call base class constructors. So
Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness) {
readinessScore = readiness;
recoveryScore = recovery;
};
should really be
Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness)
: Person(name, age, experience) {
readinessScore = readiness;
recoveryScore = recovery;
}
If you do that then the Person::Person() constructor isn't required because you are explicitly calling the Person::Person(std::string pName, int pAge, int pExperience); constructor.
Of course you should be using initialization lists everywhere. So the above code is even better written like this
Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness)
: Person(name, age, experience)
, readinessScore(readiness)
, recoveryScore(recovery) {
}
If you don't call the base class construcor from an initialization list, then the default base class constructor will be called implicitly. I guess this is why you've decalred all these default constructors. But if you code it like I show you above then you (probably) don't need all these default constructors.
Related
Learning C++ and getting error "no matching function for call to 'Weapon::Weapon(int, const char [4], int, int)'" when creating a weapon istance
Weapon MyWeapon(1,"test",4,1);
Where is the bug?
item.h
#ifndef ITEM_H
#define ITEM_H
#include <string>
#include <iostream>
using namespace std;
class Item
{
public:
string name;
int weight;
int value;
Item(string n, int w, int v);
};
#endif
item.cpp
#include <iostream>
#include <string>
#include <vector>
#include "item.h"
using namespace std;
Item::Item(string name, int weight, int value)
{
name=name;
weight=weight;
name=value;
}
weapon.h
#ifndef WEAPON_H
#define WEAPON_H
#include "item.h"
class Weapon : public Item
{
public:
int damage;
Weapon();
Weapon(int damage);
};
#endif
weapon.cpp
#include <iostream>
#include <string>
#include <vector>
#include "weapon.h"
using namespace std;
Weapon::Weapon( damage): Item(name, weight, value){}
You need to explicitly specify all the Item arguments in the Weapon constructor.
Change the header like this:
Weapon(int damage, string n, int w, int v);
And the implementation like that:
Weapon::Weapon(int damage, string n, int w, int v): damage(damage), Item(name, weight, value){}
You're getting "no matching function for call to Weapon::Weapon(int, const char [4], int, int)" because there isn't a constructor with that signature. Change the Weapon definition to something like this:
class Weapon : public Item
{
public:
int damage;
Weapon(int damage, std::string name, int weight, int value);
};
Then fix up weapon.cpp to send those arguments to the Item constructor:
Weapon::Weapon(int damage, std::string name, int weight, int value) :
Item(name, weight, value),
damage(damage)
{ }
Also a quick side note: using namespace std is considered a bad practice.
So I am using Visual Studio to make a simple program for intro to Object Oriented Programming. I am using C++ language to do this, in Netbeans with JAVA OPP isn't so complicated but I am having trouble here. I have to make a simple Object, I chose to make my object called Movie. I made a Movie.h and Movie.cpp file. I included .h's extensions to my Movie.cpp and main.cpp but when I create the object in my main or try to i keep getting errors and underlines because my .cpp file is not recognizing my variables declared in .h, it keeps saying the variable is undefined.
So visual studio's wants to help me out and I followed their method so in my .h files they said they will define my methods for me, meaning they will set it up for me and when I clicked it i get this format
string Movie::getName()
{
return string();
}
While I am using this format for this function
string getName(){
return name;
}
My variables keeps getting red underlined saying they are undefined.
My Movie.h file
#pragma once
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z );
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
My Movie.cpp File
#include "Movie.h"
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void setLength(int x) {
length = x;
}
void setRating(double y) {
rating = y;
}
void setName(string z) {
name = z;
}
//Get functions
int getLength() {
return length;
}
double getRating() {
return rating;
}
string getName() {
return name;
}
My .main
#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1();
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Now I havent worked on my toString method yet but I can't because I cant figure out what the real problem is, why is my .cpp not recognizing my variables? Is my format wrong? Is the visual studio's format right because I ran with their format and got bunch of errors as well or am I declaring them wrong or something? Thank you!
Main.cpp
//#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1;
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Movie.h
#pragma once
#ifndef Movie_H
#define Movie_H
#include <string>
#include <iostream>
using namespace std;
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z);
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
#endif
Movie.cpp
#include "Movie.h"
//#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void Movie::setLength(int x) {
length = x;
}
void Movie::setRating(double y) {
rating = y;
}
void Movie::setName(string z) {
name = z;
}
//Get functions
int Movie::getLength() {
return length;
}
double Movie::getRating() {
return rating;
}
string Movie::getName() {
return name;
}
Hello Stack Overflow!
I have encountered a problem which I have tried to solve but have not succeeded with, therefore I am turning my head towards you programmers hoping for answers.
What I am suspecting the problem is, is that it has to do with my main.cpp which I haven't started coding in yet for reasons. Could be very wrong though.
Any tips/hints are appreciated!
The error is about this part located in the BankFunctions header
// Vector
static std::vector<accounts> users;
The error messages are listed at the bottom.
I have included the two class header files.
Main.cpp:
// Classes
#include "BankFunctions.h" // Handles the bank functions
#include "accounts.h" // Handles the customer accounts
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
int main() {
return 0;
}
Class 1: Accounts
Header:
#pragma once
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
// Classes
#include "BankFunctions.h"
class accounts
{
public:
// Constructor
accounts(
unsigned int newId,
unsigned int newAge,
unsigned int newSSN,
std::string newFirstName,
std::string newLastName,
std::string newAdress,
std::string newEmail,
double newBalance
);
// Overload constructor
accounts(std::string eId, std::string eNAge, std::string eSSN, std::string eFName, std::string eLName, std::string eEmail, std::string eAdress, std::string eNBalance);
// Mutators
inline void setId(unsigned int i) { id = i; }
inline void setAge(unsigned int a) { age = a; }
inline void setSSN(unsigned int ssn) { SSN = ssn; }
inline void setFirstName(std::string FN) { firstName = FN; }
inline void setLastName(std::string LN) { lastName = LN; }
inline void setEmail(std::string em) { email = em; }
inline void setAdress(std::string adr) { adress = adr; }
inline void setBalance(double newBalance, bool t);
// Accessors
inline unsigned int getId() const { return id; }
inline unsigned int getAge() const { return age; }
inline unsigned int getSSN() const { return SSN; }
inline std::string getFirstName() const { return firstName; }
inline std::string getLastName() const { return lastName; }
inline std::string getEmail() const { return email; }
inline std::string getAdress() const { return adress; }
inline double getBalance() const { return balance; }
private:
// Customer account details
unsigned int id;
unsigned int age;
unsigned int SSN; // Social Security Number
std::string firstName;
std::string lastName;
std::string adress;
std::string email;
double balance;
};
Class 2: BankFunctions
Header:
#pragma once
// Libraries
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <sstream>
// Classes
#include "accounts.h"
class BankFunctions
{
public:
BankFunctions();
static void loadVector(); // Loads in customer account information into objects that get stored into a vector. (All the existing accounts get loaded in)
static void newCustomer(); // Create new customer account
static void existingCustomer(); // View customer account
static void deposit(unsigned int accId); // Deposit money function
static void withdraw(unsigned int accId); // Withdraw money function
// Edit customer account
static void editCustomerDetails(unsigned int accId);
// Related Functions
static void editAge(unsigned int accId);
static void editSSN(unsigned int accId);
static void editFirstName(unsigned int accId);
static void editLastName(unsigned int accId);
static void editAdress(unsigned int accId);
static void editEmail(unsigned int accId);
static void editBalance(unsigned int accId);
private:
// Vector
static std::vector<accounts> users;
static unsigned int amountOfAccounts;
};
ERROR Message:
...bankfunctions.h(36): error C2065: 'accounts': undeclared identifier
...bankfunctions.h(36): error C2923: 'std::vector': 'accounts' is not a valid template type argument for parameter '_Ty'
...bankfunctions.h(36): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type
Try just to remove the include BankFunctions.h from the accounts.h.
The problem, I think, is that an include performs simply an inserting of the whole header file at the position where the include is done, but pragma once ensures that you do not include a header twice into a cpp file.
When your headers include each other it is possible to get the class definition of accounts past the line 36 of BankFunctions.h where you need it.
You may also put an extra class accounts; on the very top of main.cpp. This should also work, but is not a pretty solution.
I am having a problem creating a simple class object. I created a small program to simulate the problem. I have a class "Person" with data members string name, string eye_color, and int pets. When I call Person new_person("Bob", "Blue", 3), my debugger shows this as the new_person's value:
{name=""eye_color=""pets=-858993460}
I'm looking at previous projects where I had no problems with this and am not spotting anything...What am I missing?
person.h
#include <iostream>
#include <string>
class Person
{
public:
Person(std::string name, std::string eye_color, int pets);
~Person();
std::string name;
std::string eye_color;
int pets;
};
person.cpp
#include "person.h"
Person::Person(std::string name, std::string eye_color, int pets)
{
this->name;
this->eye_color;
this->pets;
}
Person::~Person(){}
city.h
#include "person.h"
class City
{
public:
City();
~City();
void addPerson();
};
city.cpp
#include "city.h"
City::City(){}
City::~City(){}
void City::addPerson(){
Person new_person("Bob", "Blue", 3);
}
main.cpp
#include "city.h"
int main(){
City myCity;
myCity.addPerson();
}
It doesn't look like you are actually assigning the values in the Person class so that is why you are getting random values for those data members.
It should be:
Person::Person(std::string name, std::string eye_color, int pets)
{
this->name = name;
this->eye_color = eye_color;
this->pets = pets;
}
I'm trying to learn C++ and currently I'm trying to know how to implement an object composition in this language.
I have a Character class which is inherited by a Hero and a Monster class.
A Character has a NormalAbility and a SpecialAbility.
I've made the NormalAbility and SpecialAbility classes and both are inheriting an Ability superclass.
My problem is that when I put the #include "Character.h" in Ability.h the normalAbility and specialAbility variables in Character.h don't get recognized as their respected classes. Errors such as "syntax error : identifier string" shows in the headers of both Ability inherited classes
Here's my code:
Character.h
#pragma once
#include <string>
#include "NormalAbility.h"
#include "SpecialAbility.h"
using namespace std;
class Character
{
public:
Character(string name, string type, int hp, NormalAbility na,
SpecialAbility sa);
bool isDead();
void damage(int amt);
void heal(int amt);
void attack(Character* c, int amt);
private:
string name;
string type;
int hp;
int maxHp;
NormalAbility* normalAblity;
SpecialAbility* specialAbility;
}
Character.cpp
#include "Character.h"
#include <iostream>
Character::Character(string name, string type, int hp, NormalAbility* na,
SpecialAbility* sa)
{
this->name = name;
this->type = type;
this->maxHp = hp;
this->hp = hp;
normalAbility = na;
specialAbility = sa;
}
bool Character::isDead(){
return hp <= 0;
}
void Character::damage(int amt){
if (hp > 0){
hp -= amt;
}
else{
hp = 0;
}
}
void Character::heal(int amt){
if (hp + amt > maxHp){
hp = maxHp;
}
else{
hp += amt;
}
}
void Character::attack(Character* c, int amt){
c->damage(amt);
}
Hero.h
#pragma once
#include "Character.h"
#include <string>
using namespace std;
class Hero :
public Character
{
public:
Hero(string name, int hp);
}
Hero.cpp
#include "Hero.h"
#include <iostream>
Hero::Hero(string name, int hp)
: Character(name, "Hero", hp)
{
}
Ability.h
#pragma once
#include <string>
#include "Character.h"
using namespace std;
class Ability
{
public:
Ability(string name, string type, Character* owner);
void levelUp();
private:
string name;
string type;
int level;
Character* owner;
}
Ability.cpp
#include "Ability.h"
Ability::Ability(string name, string type, Character* owner)
{
this->name = name;
this->type = type;
this->owner = owner;
level = 1;
}
void Ability::levelUp(){
level++;
}
NormalAbility.h
#pragma once
#include "Ability.h"
#include <string>
using namespace std;
class NormalAbility :
public Ability
{
public:
NormalAbility(string name);
}
NormalAbility.cpp
#pragma once
#include "NormalAbility.h"
#include <string>
using namespace std;
NormalAbility::NormalAbility(string name) : Ability(name, "Normal")
{
//some codes
}
This way you avoid the circular include of the .h files, because you're including it in the .cpp, and in the .h you're saying that, "Character exists but don't care about his definition right now"
Ability.h
#pragma once
#include <string>
class Character;
using namespace std;
class Ability
{
public:
Ability(string name, string type, Character* owner);
void levelUp();
private:
string name;
string type;
int level;
Character* owner;
}
Ability.cpp
#include "Ability.h"
#include "Character.h"
Ability::Ability(string name, string type, Character* owner)
{
this->name = name;
this->type = type;
this->owner = owner;
level = 1;
}
void Ability::levelUp(){
level++;
}