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.
Related
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.
I keep getting error: undefined reference to 'Company::budget'.
My method is set so as to take the value of company's (any created) budget and subtract Employees' salary from it. I keep getting this problem. Tried both pointers and let's say "normal calling". Ok, there's the code snippet: (rest of it works)
company.h
#include <iostream>
#include <cstdlib>
#include <list>
#include <vector>
#include "employee.h"
using namespace std;
class Company
{
public:
Company* comp;
void hire(Employee& emp, float putSalary);
void fire(Employee& emp);
void endOfMonth(Company& comp);
Company(float);
// static float moneyamount;
private:
static float budget;
vector <Employee>* Employees;
};
company.cpp
void Company::endOfMonth(Company& comp)
{
for (iterat=0; iterat < Employees->size() ; iterat++)
{
cout << (*Employees)[iterat].fullName << endl;
cout << (*Employees)[iterat].getSalary() << endl;
comp.budget = comp.budget - (*Employees)[iterat].getSalary();
}
}
You are missing the definition of the static class data member. Add the following line to the file company.cpp:
float Company::budget;
Static class members variables are static over all instances of a class. So if you have two instances of one class, they share the static variable. Also, these variables are also valid even when there is no instance of the class. So, static member functions may use static member variables. That is the reason why they must defined somewhere outside the class in the object file.
You define it and reserve the necessary space for it in memory at the top level of you .cpp file:
float Company::budget;
I'm trying to make a program involving files assign2.cpp, Player.h, Player.cpp, Team.h, Team.cpp which reads data from a txt file on player info (like hits, atBat, position, name and number) and displays it out into assign2.cpp. assign2.cpp is what contains int main() and is suppose to contain very little code because relies on the other files to do the work.
The error:
request for member getName which is of non-class type ‘char’...
Please help, I've been trying to find the issue and can never do so. The compilation failure :
In file included from Team.cpp:1:0:
Team.h:34:11: warning: extra tokens at end of #endif directive [enabled by default]
Team.cpp: In constructor ‘Team::Team()’:
Team.cpp:15:5: warning: unused variable ‘numPlayers’ [-Wunused-variable]
Team.cpp: In member function ‘void Team::sortByName()’:
Team.cpp:49:56: error: request for member ‘getName’ in ‘((Team*)this
-> Team::playerObject[(j + -1)]’, which is of non-class type ‘char’
Team.cpp:49:74: error: request for member ‘getName’ in ‘bucket’, which is of non-class type ‘int’
Team.cpp: In member function ‘void Team::print()’:
Team.cpp:63:18: error: request for member ‘print’ in ‘((Team*)this)- >Team::playerObject[i]’, which is of non-class type ‘char’
make: *** [Team.o] Error 1
Team.h
#ifndef TEAM_H
#define TEAM_H
#include "Player.h"
class Team
{
private:
char playerObject[40];
int numPlayers; // specifies the number of Player objects
// actually stored in the array
void readPlayerData();
void sortByName();
public:
Team();
Team(char*);
void print();
};
#endif / *Team.h* /
Team.cpp
#include "Team.h"
#include <cstring>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <cstdlib>
using namespace std;
Team::Team()
{
strcpy (playerObject,"");
int numPlayers = 0;
}
Team::Team(char* newPlayerObject)
{
strncpy(playerObject, newPlayerObject, 40);
readPlayerData();
}
void Team::readPlayerData()
{
ifstream inFile;
inFile.open("gamestats.txt");
if (!inFile){
cout << "Error, couldn't open file";
exit(1);
}
inFile.read((char*) this, sizeof(Team));
inFile.close();
}
void Team::sortByName()
{
int i, j;
int bucket;
for (i = 1; i < numPlayers; i++)
{
bucket = playerObject[i];
for (j = i; (j > 0) && (strcmp(playerObject[j-1].getName(), bucket.getName()) > 0); j--)
playerObject[j] = playerObject[j-1];
playerObject[j] = bucket;
}
}
Player.h (incase anyone needs it)
#ifndef PLAYER_H
#define PLAYER_H
class Player
{
// Data members and method prototypes for the Player class go here
private:
int number;
char name[26];
char position[3];
int hits;
int atBats;
double battingAverage;
public:
Player();
Player(int, char*, char*, int, int);
char* getName();
char* getPosition();
int getNumber();
int getHits();
int getAtBats();
double getBattingAverage();
void print();
void setAtBats(int);
void setHits(int);
};
#endif
I'm very stuck, Thanks in advance.
In the Team constructor on this line
playerObject = newPlayerObject;
you're trying to assign a value of type char* to a member of type char[40], which doesn't work, since they are two different types. In any case, you probably would need to copy the data from the input instead of just trying to hold the pointer internally. Something like
strncpy(playerObject, newPlayerObject, 40);
Generally, you will always be able to assign a char[N] to a char*, but not the other way around, but that's just because C++ will automatically convert the char[N] to a char*, they are still different types.
Your declaration is:
char playerObject[40];
And your constructor reads:
Team::Team(char* newPlayerObject)
{
playerObject = newPlayerObject;
The error message you referenced in the title of this question obviously comes from here, and it is self explanatory. An array and a pointer are two completely different, incompatible types, when it comes to this kind of an assignment.
What you need to do depends entirely on what you expect to happen, and what your specifications are.
A) You could be trying to initialize the array from the character pointer, in which case you'll probably want to use strcpy(). Of course, you have to make sure that the string, including the null byte terminator, does not exceed 40 bytes, otherwise this will be undefined behavior.
Incidently, this is what you did in your default constructor:
Team::Team()
{
strcpy (playerObject,"");
}
B) Or, your playerObject class member should, perhaps, be a char * instead, and should be either assigned, just like that, or perhaps strdup()ed. In which case your default constructor will probably need to do the same.
Whichever one is the right answer for you depends entirely on your requirements, that you will have to figure out yourself.
I'm having trouble implementing a state machine for class. I keep getting the errors:
state.cpp:5: error: have0 was not declared in this scope
state.cpp:10: error: redefinition of State* Have0State::process(std::string)
state.h:18: error: virtual State* Have0State::process(std::string) previously defined here
I'm trying to get the Have0State to work before I continue onto the rest of the machine, hence the sparse code.
state.h:
#ifndef STATE_H
#define STATE_H
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <iostream>
class State{
public:
State(){};
virtual State* process(std::string input) = 0;
};
class Have0State: public State {
public:
Have0State():State(){};
virtual State* process(std::string input);
}have0;
#endif
state.cpp:
#include "state.h"
using namespace std;
State *currentState = &have0;
State* Have0State::process(string input){
if(input == "quarter"){
cout << "cool" << endl;
}
return &have0;
}
int main(int argc, char** argv) {
string input;
//get input
cin >> input;
while (input != "exit") {
currentState = currentState->process(input);
//get input
cin >> input;
}
return 0;
};
I've tried defining the process function as Have0State::State::process(string input) but that didn't work either. Any clarification on how function pointers are supposed to work, especially in the context of subclass member functions, I would greatly appreciate it.
EDIT: Also, what exactly is the have0 declaration at the end of the Have0State class declaration in the state.h file? It doesn't have an explicitly stated type; is it implied that it is of type Have0State??
There aren't any function pointers in your example. Also, like Marciej, I am able to compile (and run) this code.
But, since you asked, the 'have0' declaration simply declares an instance of the class. A class definition can be followed by 0 or more of these declarations (as well as initializers):
class Thing {...} one, another, many[3] = { Thing(1), Thing(2), Thing(3) };
the same as for any other type:
int counter = 0, flag = 0x80, limit = 500;
The possibility of this optional declarator list is why class, struct, union, and enum definitions must be followed with a semi-colon (to terminate the list).
But, as Karthik said, defining a variable in a header will cause "duplicate definition" errors at link time, if the header is included in more than one .cpp file. IMO it's fine though to use this technique to define and declare private objects in a .cpp file (rather than a .h file).
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.