fillVector with objects C++ [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I have a class Player and a class Team.
I want to create a vector of Players in constructor of class Team. I have written a method called fillVector witch creates all the players. I have added method fillVector into class Team witch is shown bellow. I do not think class Player is necessary.
When I compile my programm with codeblocks there is the following error:
Team.cpp|9|undefined reference to `Team::fillTeamVector(std::vector >&)'|
|error: ld returned 1 exit status|
This is the Team.cpp code :
#include "Team.h"
#include <vector>
#include <iostream>
#include "Player.h"
Team::Team()
{
vector<Player> team;
fillTeamVector(team);
}
void fillTeamVector(vector<Player>& team){
cout << "How many players are in the team? " <<endl;
string name;
int teamSize,x,y,num,target_line;
cin >> teamSize;
for (int i=0 ; i<=teamSize ; i++){
cout << "Give player's name" << endl;
cin >> name;
cout << "Give player's number" << endl;
cin >> num;
cout << "Give player's x position" << endl;
cin >> x;
cout << "Give player's y position" << endl;
cin >> y;
cout << "Give player's target line" << endl;
cin >> target_line;
Player newPlayer(name,num,x,y,target_line);
team.push_back(newPlayer);
}
}
This is the Team.h code :
#ifndef TEAM_H
#define TEAM_H
#include <iostream>
#include <vector>
#include "Player.h"
using namespace std;
class Team
{
public:
Team();
void fillTeamVector(vector<Player>&);
};
#endif // TEAM_H
This is the main.cpp code :
#include <iostream>
#include <stdio.h>
#include "Player.h"
#include "Team.h"
using namespace std;
int main()
{
Team team;
return 0;
}

You have defined "fillTeamVector" as a free function in team.cpp, while it's a member function in the .h.
So fillTeamVector method of Team does not exist, but is called, hence the error.
In team.cpp, replace this :
void fillTeamVector(vector& team)
by this :
void Team::fillTeamVector(vector& team)

You've declared the function:
void fillTeamVector(vector<Player>& team){ // ...
But you forgot the "class-scope", that because that function is a member-function so you have to add:
void Team::fillTeamVector(vector<Player>& team){ // ...
Exactly what you've done for the constructor (Team::Team()).

You wrote a function called fillTeamVector.
This is not the same thing as a class method called Team::fillTeamVector, which is called by the constructor.

Related

Array of objects prints blank string

(The issue has been solved and the solution has been added as comment line to main.cpp)
The problem I'm having is described in the main.cpp file. I already checked another questions about this and none of them really helped.
I'm trying to create a console application with C++ where you can add BOOKS to the LIBRARY. In the library class, there is a displayInfo() function which displays the info of a particular book. It can display integer or double valued informations without having a problem however it is having a trouble when I try to display string typed informations. It just prints blank. Let me give you a little sample of my code.
Here is Book.h
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book
{
friend class Library;
public:
void setName();
std::string getName();
private:
std::string nameOfBook;
};
#endif
Book.cpp
#include "Book.h"
#include <iostream>
#include <string>
using namespace std;
void Book::setName()
{
string nameOfBook;
cout << "Please enter the name of the book: ";
cin >> nameOfBook;
this->nameOfBook = nameOfBook;
}
string Book::getName()
{
return nameOfBook;
}
Library.h
#ifndef LIBRARY_H
#define LIBRARY_H
#include "Book.h"
#include <array>
class Library
{
private:
std::array <Book , 10> bookArray; // I have to use this
public:
void addBook();
void displayInfo();
};
#endif
Library.cpp
#include "Library.h"
#include "Book.h"
#include <iostream>
#include <string>
#include <array>
using namespace std;
void Library::addBook()
{
bookArray[0].setName();
}
void Library::displayInfo()
{
cout << "The book: " << bookArray[0].getName() << endl;
}
And main.cpp
#include <iostream>
#include "Book.h"
#include "Library.h"
#include <string>
#include <array>
using namespace std;
int main()
{
// The problem is solved
// Create the objects out of the loop
// Library obj1 <---- this solved it
while (true)
{
int number; // Ask user what to do
cout << "Press 1 to add a book\n"
<< "Press 2 to display info\n"
<< "Press 0 to quit\n";
cin >> number;
if (number == 1) // Add a book
{
Library obj1; // <------ THIS IS WRONG
obj1.addBook(); // Consider we named the book as 'Fly'
}
else if (number == 2)
{
Library obj1; // <------ THIS IS WRONG
obj1.displayInfo(); // ******* The output I'm expecting is The Book: Fly
// But instead, it only gives me The Book:
// I have 4 more strings like this in the main project and all of them have the same problem
}
else if (number == 0) // Everything else
{
break;
}
else
{
cout << "Wrong input\n";
continue;
}
}
}
Edit:
I coded this with VS Code and compiled it with MinGW (8.2.0) if it matters.
one problem in your code is that you have many instances of a library class so addBook is landing in one object and displayInfo in a new one (empty one)
you have to:
int main()
{
Library obj1; // declare the lib in scope for all the cases
while (true)
{
int number; // Ask user what to do
cout << "Press 1 to add a book\n"
<< "Press 2 to display info\n"
<< "Press 0 to quit\n";
cin >> number;
if (number == 1) // Add a book
{
obj1.addBook(); // Consider we named the book as 'Fly'
}
else if (number == 2)
{
//Library obj1;
obj1.displayInfo(); // ******* The output I'm expecting is The Book: Fly
// But instead, it only gives me The Book:
// I have 4 more strings like this in the main project and all of them have the same problem
}
else if (number == 0) // Everything else
{
break;
}
else
{
cout << "Wrong input\n";
continue;
}
}
}
u are creating the object again in every iteration of the loop. therefore overwriting the old object that has been given a name.

How to set cin to a member function of a class in C++?

I am making a small console game and I have a player class with private integers for the stats and a private string for the name. What I want to do is to ask the user for their name, and store that into the private name variable in the player class. I got an error stating:
error: no match for 'operator>>'
(operand types are 'std::istream {aka std::basic_istream<char>}' and 'void')
Here is my code:
main.cpp
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
Player the_player;
string name;
cout << "You wake up in a cold sweat. Do you not remember anything \n";
cout << "Do you remember your name? \n";
cin >> the_player.setName(name);
cout << "Your name is: " << the_player.getName() << "?\n";
return 0;
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
using namespace std;
class Player {
public:
Player();
void setName(string SetAlias);
string getName();
private:
string name;
};
#endif // PLAYER_H
Player.cpp
#include "Player.h"
#include <string>
#include <iostream>
Player::Player() {
}
void Player::setName(string setAlias) {
name = setAlias;
}
string Player::getName() {
return name;
}
The return type for the setName function is void, not a string. So you have to store first the variable in a string, and then pass it to the function.
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
Player the_player;
cout << "You wake up in a cold sweat. Do you not remember anything \n";
cout << "Do you remember your name? \n";
string name;
cin >> name;
the_player.setName(name);
cout << "Your name is: " << the_player.getName() << "?\n";
return 0;
}
If you surely want use function, should return object reference.
string& Player::getNamePtr() {
return name;
}
cin >> the_player.getNamePtr();
Firstly, try to get the value in the name variable from the user and then call the setName method of the class Player:
cin>>name;
the_player.setName(name);

Why doesn't the stl vector store my class object with the correct values?

I'm using a vector v to store a class object called t_display. I've checked with the debugger and I can see the display object is instantiated correctly. However, when I pass it into the vector with v.push_back() or v.insert(), it stores every value as -842150451. I can see this in the print statements and in the debugger and I cant figure out why it's storing the object this way. Additionally, its storing the integer values this way every time i execute the program, which leads me to believe its not a memory issue although i cant be sure. I've checked all over stack overflow and cppreference. Any advice would be appreciated.
here is the class where I create the object and pass into the vector. I just want the vector to contain the newly made object t_display at the first element.
Animation.cpp
#include <crtdbg.h>
#include <iostream>
#include <string>
#include <vector>
#include <forward_list>
using namespace std;
#include "Display.h"
#include "Animation.h"
void Animation::InsertFrame() {
int numDisplays;
vector <Display>v;
int p_x;
int p_y=0;
int p_duration=0;
string p_name;
string frameName;
cout << "Insert a Frame in the Animation\nPlease enter the Frame filename" << endl;
cin >> frameName;
cout << "Entering the Frame Displays (the sets of dimensions and durations) " << endl;
cout << "Please enter the number of Displays :" << endl;
cin >> numDisplays;
vector <Display>::iterator it;
it = v.begin();
while (numDisplays > 0) {
cout << "Please enter pixel x for Display #0 pixel_x:";
cin >> p_x;
cout << "\nPlease enter pixel y for Display #0 pixel_y:" << endl;
cin >> p_y;
cout << "\nPlease enter the duration sec for this Display :" << endl;
cin >> p_duration;
cout << "\nPlease enter the name for this Display : " << endl;
cin >> p_name;
Display t_display = Display(p_x, p_y, p_duration, (char *)p_name.c_str());
//it = v.insert(it, t_display);
v.push_back(t_display);
numDisplays--;
}
Display.h
// Display.h
#pragma once
class Display
{
int pixel_x;
int pixel_y;
int duration;
char* name;
public:
Display(int x, int y, int duration, char* name);
Display(const Display&);
~Display();
friend ostream& operator<<(ostream&, Display&);
};
## Display.cpp ##
#include <crtdbg.h>
#include <iostream>
#include <string>
#include <vector>
#include <forward_list>
using namespace std;
#include "Display.h"
int q = 0;
Display::Display(int x, int y, int d, char* n):pixel_x(x), pixel_y(y), duration(d), name(n) {}
Display::Display(const Display&) {
}
Display::~Display() {
}
It turns out that I didn't write a copy constructor.

Unable to change private variable's content

main.cpp :
#include <iostream>
#include <string>
#include "Players.h"
using namespace std;
int main ()
{
cout << "**** Welcome to Leviathan's first TicTacToe Game! ****\n\n";
Players getNamesobject;
Players printNamesobject;
getNamesobject.getPlayersNames();
printNamesobject.printPlayersNames();
}
Players.h:
#ifndef PLAYERS_H
#define PLAYERS_H
class Players
{
public:
void getPlayersNames();
void printPlayersNames();
private:
std::string _player1Name;
std::string _player2Name;
};
#endif // PLAYERS_H
Players.cpp :
#include <iostream>
#include <string>
#include "Players.h"
using namespace std;
void Players::getPlayersNames()
{
string p1,p2;
cout << "Enter player 1 name : ";
cin >> p1;
cout << "\nEnter player 2 name : ";
cin >> p2;
_player1Name = p1;
_player2Name = p2;
}
void Players::printPlayersNames()
{
cout << "Alright " << _player1Name << " and " << _player2Name <<", the game has begun!\n\n";
}
When i run this, and enter two names, the _player1Name and _player2Name variables don't get changed. I've tried setting them a string manually and they get printed normally. Can anyone explain what's wrong here? It seems like getPlayerNames function can't change the private variables?
It's because you have two different objects!
One that you set the member variables in (through the getPlayersNames function), and another unrelated object you use to print a different set of variables.
You should have a single object, and call getPlayersNames and printPlayersNames on that single object. Like
Players playersObject;
playersObject.getPlayersNames();
playersObject.printPlayersNames();
Each instance of the Players object you create will have its own set of member variables that are tied to that single object, member variables are not shared between objects (unless you make them static).

Can anyone explain my error from code blocks C++ [duplicate]

This question already has answers here:
Problems importing libraries to my c++ project, how to fix this?
(2 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm trying to create a classes in separate files using codeblocks. I get the error in function '_start' undefined reference to 'main'. I'm sure its a linkage problem but can't see where. In my program I’m trying to get a die, let the user decide how many sides the dice has, then roll the die a user specified amount of times.
die.h file///////////////////////////////////
#include <iostream>
#include <string>
#ifndef DIE_H
#define DIE_H
using namespace std;
class die{
public:
die();//function prototype
int numsides;//member
void setNumsides(int numsides_);//setter
int getNumsides();// getter for size of dice
int value;
void setValue(int value_, int numsides_);
int getValue();
int roll;
void setroll(int roll_);
int getroll();
};
#endif// DIE_H
die.cpp//////////////////////////////////////////
#include "die.h"
#include <iostream>
#include <string>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
die::die()
{
//ctor
}
void die::setroll(int roll_)
{
roll=roll_;
}
int die::getroll()//amount o rolls
{
cout << "enter the ammont of rolls you would like" << endl;
cin >> roll;//amount of rolls you want
return roll;
}
void die::setValue(int value_, int numsides_)
{
value=value_;
numsides=numsides_;
}
int die::getValue()//get value function
{
//int roll;
value = (rand() % numsides) + 1;//sets roll value
return value;
}
void die::setNumsides(int numsides_)
{
numsides=numsides_;
}
int die::getNumsides()//get num of sides
{
cout << "how big of a dice would you like to roll " << endl;
cin >> numsides;//use this to determine dice
if(numsides < 4){//if dice is less than 4
cout << "Error has to be bigger than " << numsides << endl;
numsides = 6;//change to six sided dice
}
return numsides;
}
exercise1.cpp my main class/////////////////////////////////////
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include "die.h"
using namespace std;
int main()
{
die mydice;//create an object dice
mydice.getNumsides();//gets sides of dice
mydice.getValue();//gets amount of rolls
mydice.getroll();//rolls the dice value times
return 0;
}