C++ Constructor won't pass string - no constructor defined - c++

I am trying to create an object of "Player" inside "PlayerManager" and I am getting the error in VS2010:
Error 1 error C2512: 'Player::Player' : no appropriate default constructor available c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory 631 1 Server
Player.h:
#ifndef _PLAYER_H
#define _PLAYER_H
#include <iostream>
#include <string>
using namespace std;
class Player
{
public:
Player(const string &name);
~Player(void);
private:
string name_;
};
#endif
Here is the constructor in Player.cpp:
Player::Player(const string &name)
{
}
PlayerManager.h:
'#ifndef _PLAYERMANAGER_H
#define _PLAYERMANAGER_H
#include <string>
#include <vector>
#include <iostream>
#include "Player.h"
using namespace std;
class PlayerManager
{
public:
PlayerManager(void);
~PlayerManager(void);
private:
vector<Player> players;
};
#endif'
Here is where I create the object in PlayerManager.cpp:
PlayerManager::PlayerManager(void)
{
Player test("Hello");
players.resize(1000);
for(int i=0; i < 960; i++){
players.push_back(test);
}
}
I don't understand why it is ignoring the string "Hello", I have tried creating a string object but gives same error.
I have also tried without adding the const & in the constructor but gives same error.
Any help would be appreciated, spend hours searching for an answer. Apologies if the layout is incorrect as this is my first time asking a question.

The class std::vector requires that the class you use it with has a default constructor1. You'll need to provide one for your class.
If you really don't want to provide one, you can give an instance of your class to vector in it's constructor call, so that it will use that instance instead of trying to default-construct one:
vector v(initialsize, Player("")); // or you can pass whatever string you want the default item to have
If the vector that you are using to store Players is a member variable, you'll need to pass it the default Player to use in the initialiser list:
PlayerManager::PlayerManager() : players(initialsize, Player("")) { // assuming the vector is named players
....
}
1 As R. Martinho Fernandes and Kerrek SB have pointed out in the comments, a default constructor is only required for this particular constructor of vector (the one that takes an initial size and when you don't give it a default instance) and the member function resize when called with a single argument. If you use the constructor that takes iterators or a const Allocator&, or if you use resize with the second argument, then you don't need a DC.

Related

How to make it work, problem with 2 classes

i have problem with my small project. I have two classes in it.
Problem:
error: 'Display' was not declared in this scope
Display is a class. Here is code:
//main.cpp
#include <iostream>
#include "Display.h"
#include "Polynomial.h"
using namespace std;
int main()
{
Polynomial prr;
prr.show();
cout<<endl;
cout<<"Enter x= ";
int x;
cin>>x;
cout<<endl;
cout<<"value for x="<<x<<endl<<"y="<<prr.value(x);
Display aa; // this doesn't work
//abc.show();
return 0;
}
//Display.h
#ifndef DISPLAY_H
#define DISPLAY_H
class Display
{
std::vector <vector <char> > graph;
public:
Display(int a, int b);
//friend void lay(Polynomial abc,Display cba);
//void show();
};
#endif // DISPLAY_H
I was thinking that maybe vectors are doing problems. I tested it without vectors, but it didn't change anthing.
//Display.cpp
#include "Display.h"
#include <iostream>
using namespace std;
Display::Display(int a, int b)
{
//ctor
if(a%2==0)
a++;
if(b%2==0)
b++;
vector <char> help;
vector <char> mid;
for(int i=0; i<b; i++)
{
mid.push_back('-');
if(i==(b+1)/2)
help.push_back('|');
else
help.push_back(' ');
}
for(int i=0; i<a; i++)
{
if(i==(a+1)/2)
graph.push_back(mid);
else
graph.push_back(help);
}
}
Now it's Polynomial class it's working fine, but Display class no, and i don't know why.
//Polynomial.h
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <vector>
//class Display;
class Polynomial
{...}
#endif // POLYNOMIAL_H
//Polynomial.cpp
#include "Polynomial.h"
#include <iostream>
#include <windows.h>
#include <cmath>
using namespace std;
// constructors and methods here
// everything here working fine
Edit:
After few tries i am one step back,
Now in Display.h
i have error :
error: 'vector' does not name a type
So i included vector lib.
But it didn't help.
Error Number 1:
You defined a constructor with 2 parameters
Display(int a, int b);
But when you call
Display aa;
Compiler try to instantiate a Display object with a default constructor, that you disabled defining a custom costructor;
you have 2 possibilities:
Adding a default constructor like
Display() = default;
or
Display() { /* do whatever you want to init with default parameter */}
Instantiate your variable using the constructor you defined
Display aa{0,0};
Error number 2:
std::vector < std::vector <char> > graph;
You declared vector<char> instead of std::vector<char>
See a Live Example
One reason is that your Display class has no default constructor, considering you're creating object like Display aa; . A default constructor is the constructor that has no arguments. Default constructors are provided implicitly by compiler as synthesized default constructor only if you don't provide any constructors to your class. If you provide your own constructors to your class, you must also explicitly provide a default constructor. So in your case, you should actually create Display object like this Display aa(argument, argument); by providing arguments. However, If you want to create object like Display aa; then add either Display () { } or Display() = default; in your Display.h file.
Considering you created object like the way I described but still getting an error, another reason could be that you're not compiling the source file that contains the Display (int,int); constructor definition (not just declaration as you did in your header file) along with the source file that contains the main function. If you did that but still getting an error in compilation, then I would assume it is a compiler issue and try adding a forward declaration class Display; which should compile the code. But the definition of Display has to be within the visible range of main function otherwise a forward declaration would do nothing.
In any case, you have to make sure the definition of your class is within the visible range of the main function that creates the class object. A class type with only declaration without a definition is called incomplete type and you cannot create an object of incomplete type. So the declaration of your Display (int,int); constructor in the Display.h is not enough. You also need a definition of that within the visible range of main function. You can either do that in the same file as main, same file as header, or a separate source file (which is the best practice) that has the complete definition of Display class, its data members, and member functions. However, you must make sure to compile that source file along with the source file containing main.

C++ Beginner : calling default vs custom constructor

Beginner here - but i was uncertain what exactly to search for this (presumably common) question.
I am working on a program where I have a given class (Dictionary). I am supposed to make a concrete class (Word) which implements Dictionary. I should mention that I am not to change anything in Dictionary.
After making a header file for Word, I define everything in word.cpp.
I am unsure if I am doing this correctly, but I make the constructor read from a given file, and store the information in a public member of Word.
(I understand that the vectors should be private, but I made it public to get to the root of this current issue)
dictionary.h
#ifndef __DICTIONARY_H__
#define __DICTIONARY_H__
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Dictionary
{
public:
Dictionary(istream&);
virtual int search(string keyword, size_t prefix_length)=0;
};
#endif /* __DICTIONARY_H__ */
word.h
#ifndef __WORD_H__
#define __WORD_H__
#include "dictionary.h"
class Word : public Dictionary{
public:
vector<string> dictionary_words;
vector<string> source_file_words;
Word(istream &file);
int search(string keyword, size_t prefix_length);
void permutation_search(string keyword, string& prefix, ofstream& fout, int& prefix_length);
};
#endif /* __WORD_H__*/
word.cpp
#include "word.h"
Word(istream& file) : Dictionary(istream& file)
{
string temp;
while (file >> temp)
{
getline(file,temp);
dictionary_words.push_back(temp);
}
}
In word.cpp, on the line "Word::Word(istream& file)", I get this error :' [Error] no matching function for call to 'Dictionary::Dictionary()'.
I've been told this is error is due to "Word's constructor invoking Dictionary's ", but I still don't quite grasp the idea well. I am not trying to use Dictionary's constructor, but Word's.
If anyone has an idea for a solution, I would also appreciate any terms related to what is causing this issue that I could look up - I wasn't even sure how to title the problem.
Your child class should invoke parent constructor, because parent object are constructed before child. So you should write something like:
Word::Word(isteam& file) : Dictionary(file)
{
...
}
Seems its better described here What are the rules for calling the superclass constructor?

ifstream attempting reference to a deleted function

I'm writing a code for a virtual tournament. The problem is that team class has an ifstream object, I understand that stream objects do not have copy constructors, therefore i converted playing8 from vector of team objects to pointer to object, So that team objects will not be copied. But now I get this error
Error 16 error C2280:
'std::basic_ifstream<char,std::char_traits<char>>::basic_ifstream(const std::basic_ifstream<char,std::char_traits<char>> &)' :
attempting to reference a deleted function
c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 592 1 Assignment3
How do I resolve this without removing the ifstream object from the team class?
Here's code for tournament.h
#include "team.h"
class Tournament
{
std::ofstream out_file;
std::ifstream in_file;
std::vector<team> teams;
std::vector<team*> playing8;
public:
void schedule();
void schedule2();
void tfinal();
void selectPlaying8();
void rankTeams();
void match(int,int);
Tournament();
~Tournament();
};
Code for the tournament constructor:
Tournament::Tournament()
{
srand(time(NULL));
in_file.open("team_list.txt");
string input;
int noteam=0;
while (getline(in_file, input)){
noteam++;
}
in_file.close();
for (int i = 0; i < noteam;i++){
string x=to_string(i)+".csv";
team temp(x);
temp.set_teamform((6 + rand() % 5) / 10.0);
teams.push_back(temp);
}
}
Code for select playing 8:
void Tournament::selectPlaying8(){
for (int i = 0; i < 7; i++) {
playing8.push_back(&teams[i]);
playing8[i]->set_playing();
}
}
Attributes of team class
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include "Player.h"
class team
{
private:
std::ifstream in_file;
std::vector<Player> playing11;
std::string teamname;
std::vector<Player> player;
bool playing;
float matchperformance;
float teamform;
float team_rank_score;
};
I'm using visual studio express 2013.
This code
playing8.push_back(&teams[i]);
makes a copy of the team class instance pushed back using the compiler generated copy constructor. It tries simply to copy each member.
ifstream doesn't provide a copy constructor (it's deleted), hence you get this error.
To fix this you'll need to use a ifstream* pointer, or ifstream& reference.
Not every variable has to be a class variable. Generally speaking, variables should be kept in the smallest scope possible.
Keep your files as local variables, there is no need to have them as class fields.

C++ - No appropriate default constructor available [duplicate]

This question already has answers here:
"No appropriate default constructor available"--Why is the default constructor even called?
(2 answers)
Closed 7 years ago.
I'm having trouble with a very simple program. It throws the errors:
error C2512: 'Player' : no appropriate default constructor available
IntelliSense: no default constructor exists for class "Player"
I have a feeling it's got something to do with declaring the Player class as a private variable in the Game.h but I can't see why. Any help would be much appreciated.
Game.h
#pragma once
#include "Player.h"
class Game
{
public:
Game(void);
void start(void);
~Game(void);
private:
Player player;
};
Game.cpp
#include "Game.h"
Game::Game(void)
{
Player p(100);
player = p;
}
void Game::start()
{
...
}
Game::~Game(void)
{
}
Player.h
#pragma once
class Player
{
public:
Player(int);
~Player(void);
private:
int wallet;
};
Player.cpp
#include "Player.h"
#include <iostream>
using namespace std;
Player::Player(int walletAmount)
{
wallet = walletAmount;
}
Player::~Player(void)
{
}
In contrast to C#, this declaration;
Player player;
...is an instantiation of the type Player, which means that by the time you're assigning it inside the constructor, it has already been constructed without a parameter.
What you need to do is to tell the class how to initialize player in what is called an initializer list that you append to the constructor header;
Game::Game(void) : player(100)
{
...
...which tells the compiler to use that constructor to initialise player in the first place instead of first using the default no-parameter constructor and then assigning to it.
Only Player::Player(int) exists, so you need to initialize player in Game::Game()
Game::Game(void)
: player( 100 )
{
}
When you construct instance of Game, it tries to construct its member player using the default constructor. Initialize player within the initializer list, not inside the body of constructor:
Game::Game() : player(100)
{
...
}
In general - not in this case - the error
no appropriate default constructor available
may also occur, if you forgot to include the header file for the related object.
This happened to me, so I wanted to add this solution here.

C++ - "Unspecialised class template" error with shared_ptr

I have a class Room and it holds a vector of shared_ptrs to Option objects like so:
private:
vector<shared_ptr<Option> > options;
But for some reason when I build, I get the following errors:
'shared_ptr' : unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
'std::tr1::shared_ptr' : use of class template requires template argument list
Strangely, I also have a vector of shared_ptrs, exact same syntax but there's no problem with that one.
There's also a bunch of places that bring up the error "'Option': undeclared identifier", which brings me to think it might be a problem with the Option class, but it seems to be fine. Here's the code for Option:
Option.h:
#pragma once
#include "Room.h"
#include <memory>
using namespace std;
class Option
{
protected:
int id;
char* text;
public:
Option(void);
Option(int, char*);
virtual ~Option(void);
char* getText();
int getID();
};
Option.cpp:
#include "Option.h"
#include "Room.h"
#include <memory>
using namespace std;
Option::Option(void)
{
}
Option::Option(int newID, char* newText){
id = newID;
text = newText;
}
Option::~Option(void)
{
}
char* Option::getText(){
return text;
}
int Option::getID(){
return id;
}
There is a bit of conjecture in this answer since you haven't posted the code for the Room class. I'm assuming this code
private:
vector<shared_ptr<Option> > options;
is in Room.h. Your Option.h file includes Room.h, hence the Room class gets declared before the Option class. So Option is an incomplete type when the Room class' destructor is compiled and the shared_ptr implementation tries to delete the Option object.
From the code above, I don't see why Option.h needs to include Room.h, in fact, it should be the other way around. If it does indeed need to include the file, you should be able to work around the problem by explicitly declaring Room::~Room() out-of-line in Room.cpp.
EDIT:
Turns out ~shared_ptr<T> does not require T to be a complete type. However, shared_ptr<T>( T* ) and shared_ptr<T>::reset( T* ) do, and the problem may be because some operation on the vector is invoking a call to one of these (more likely the former).
vector<shared_ptr<Option >>
You almost did that right :)
vector<shared_ptr<Option> >
It's the two > characters that, when touching, cause the strange errors you see. It is being interpreted as the >> operator.
BTW, thank you for posting your code exactly as it is rather than typing it back in and possibly hiding the mistake.