EDIT: I removed the main from Card.cpp, and run
C:\cpp>g++ Deck.cpp Card.cpp
C:\cpp>g++ Deck.cpp Card.cpp
In file included from Deck.h:8:0,
from Deck.cpp:1:
Card.h: In member function 'Card& Card::operator=(const Card&)':
Card.h:13:12: instantiated from 'void std::vector<_Tp, _Alloc>::_M_insert_aux(
std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = Card, _Alloc = std::
allocator<Card>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterat
or<Card*, std::vector<Card> >, typename std::vector<_Tp, _Alloc>::_Base::_Tp_all
oc_type::pointer = Card*]'
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:749:4: ins
tantiated from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [wit
h _Tp = Card, _Alloc = std::allocator<Card>, value_type = Card]'
Deck.cpp:7:44: instantiated from here
Card.h:13:12: error: non-static const member 'const int Card::rank', can't use d
efault assignment operator
Card.h:13:12: error: non-static const member 'const Suit Card::suit', can't use
default assignment operator
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/vector:6
9:0,
from Deck.h:4,
from Deck.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/vector.tcc: In member fun
ction 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::it
erator, const _Tp&) [with _Tp = Card, _Alloc = std::allocator<Card>, std::vector
<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<Card*, std::vector<Card>
>, typename std::vector<_Tp, _Alloc>::_Base::_Tp_alloc_type::pointer = Card*]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/vector.tcc:312:4: note: s
ynthesized method 'Card& Card::operator=(const Card&)' first required here
I don't know what the problem is. Someone else tells me that it compiled perfectly fine on their computer. Anybody know what's going on?
Card.h
#ifndef CARD_H
#define CARD_H
#include <string>
enum Suit {
SUIT_HEART,
SUIT_DIAMOND,
SUIT_CLUB,
SUIT_SPADE
};
class Card {
private:
const int rank;
const Suit suit;
static const char * ranknames[];
static const char * suitnames[];
public:
Card(int r = 1, Suit s = SUIT_HEART) : rank(r), suit(s)
{
}
int GetRank() const { return rank; };
Suit GetSuit() const { return suit; }
std::string ToString() const;
std::string SuitString() const;
std::string RankString() const;
};
#endif
Card.cpp
#include <iostream>
#include "Card.h"
#include <vector>
const char * Card::ranknames[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
const char * Card::suitnames[] = { "Hearts", "Diamonds", "Clubs", "Spaces" };
std::string Card::ToString() const {
std::string s = RankString();
s.append(" of ");
s.append(SuitString());
return s;
}
std::string Card::SuitString() const {
return suitnames[suit];
}
std::string Card::RankString() const {
return ranknames[rank-1];
}
Deck.h
#ifndef DECK_H
#define DECK_H
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include "Card.h"
#endif
Deck.cpp
#include "Deck.h"
int main() {
std::vector<Card> Deck;
for (int i = 0; i < 10 ; i++) {
Deck.push_back(Card(i+1,(Suit)((i+1)%4)));
std::cout << Deck[i].ToString() << std::endl;
}
}
Did you try specifying both modules on the command line?
g++ -o deck.exe deck.cpp card.cpp
Edit: Also, using constants that start with _ or __ is technically illegal. For include-guards, I prefer to use the form MODULE_H
You're supposed to compile and link Card.cpp and Deck.cpp together in a single executable, which I presume you don't as both files contain a main function :
g++ Deck.cpp Card.cpp
Of course, you'll have to remove one of the two main if you don't want to get into a new linker error.
Related
I'm having trouble loading a font as a static member of a custom class.
I have tried following the SFML tutorial but there are steps that I am clearly missing!
The code is the following:
#include <string>
#include <iostream>
#include <sstream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <random>
#include <chrono>
#include <math.h>
using namespace std;
using namespace sf;
class base
{
int number;
double radius;
double last_update_time;
public:
static const string FontFile = "SugarpunchDEMO.otf";
static bool Init(const string& FontFile)
{
return font.loadFromFile(FontFile);
}
CircleShape shape;
Text text;
static Font font;
void update_number(double time, double &last_update_time, int &number);
void update_radius(int number, double &radius);
base(int ini_number, double pos_x, double pos_y, double time);
void update(double time);
};
The constructor is:
base::base(int ini_number, double pos_x, double pos_y, double time){
number = ini_number;
update_radius(number, radius);
shape.setRadius(radius);
shape.setFillColor(Color::Red);
shape.setPosition(pos_x - radius, pos_y - radius);
text.setFont(font);
char name[32];
sprintf(name,"%d",number);
text.setString(name);
text.setCharacterSize(200);
text.setFillColor(sf::Color::Blue);
text.setPosition(pos_x,pos_y);
last_update_time = time;
}
The goal would be to load the font just once and to have it applied to each member of the class.
The error I got is:
In file included from base.cpp:9:0:
base.hpp:19:29: error: in-class initialization of static data member ‘const string base::FontFile’ of non-literal type
static const string FontFile = "SugarpunchDEMO.otf";
^~~~~~~~
base.hpp:19:40: error: call to non-constexpr function ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
static const string FontFile = "SugarpunchDEMO.otf";
^~~~~~~~~~~~~~~~~~~~
This issue has nothing to do with SFML as your error message states:
in-class initialization of static data member const string base::FontFile of non-literal type
Or in other words: This way of initializing a class member is only allowed for numerical values and pointers to (string) literals. You're trying to initialize a std::string object of said class.
As a solution, use const char* for FontFile or move the initialization to your implementation file as const std::string FontFile = "SugarpunchDEMO.otf";
abstract base class:
#ifndef BUILDINGORG_H
#define BUILDINGORG_H
#include <iostream>
#include <memory>
#include <vector>
class BuildingOrg
{
public:
BuildingOrg(int _id);
virtual int addBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg,
std::string _type) const;
virtual void removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg);
virtual void getInfo()=0;
private:
int id;
std::string type;
};
#endif // BUILDINGORG_H
concrete subclass:
#ifndef BUILDINGCOMPONENT_H
#define BUILDINGCOMPONENT_H
#include "buildingorg.h"
class BuildingComponent : public BuildingOrg
{
public:
BuildingComponent(int _id);
int addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg,
std::string _type) const override;
void removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg)
override;
void getInfo() override;
private:
std::vector<std::shared_ptr<BuildingOrg>> building_Org;
};
#endif // BUILDINGCOMPONENT_H
Implementation of subclass:
#include "buildingcomponent.h"
BuildingComponent::BuildingComponent(int _id):
BuildingOrg(_id)
{
}
int BuildingComponent::addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg, std::string _type) const
{
building_Org.push_back(_buildingOrg);// I am having error here
return 1;
}
void BuildingComponent::removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg)
{
}
void BuildingComponent::getInfo()
{
}
When I try to put shared pointer in my Vector I get this nasty error;
I really don't know why I am getting the error:
cpp:10: error: passing 'const std::vector<std::shared_ptr<BuildingOrg> >' as 'this' argument of 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::shared_ptr<BuildingOrg>; _Alloc = std::allocator<std::shared_ptr<BuildingOrg> >; std::vector<_Tp, _Alloc>::value_type = std::shared_ptr<BuildingOrg>]' discards qualifiers [-fpermissive]
building_Org.push_back(_buildingOrg);
I don’t understand what is it saying.
The const in int addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg, std::string _type) const override; is a promise that addBuildingComponent will not change BuildingComponent. However, it tries to modify the member variable building_Org with the push_back()...
Removing the const from addBuildingComponent() should fix the error.
The discards qualifiers part of the error message refers to the conflict with the const qualifier of the member function.
C++ template related error messages can be notoriously difficult to parse at first, but it does get easier with practice :-)
You defined BuildingComponent::addBuildingComponent method as const (i.e. that it won't change member varialbles), but you are adding passed in value to a member list (i.e. changing the member variable).
addBuildingComponent() is a const method. within its scope, *this is const, and so this->building_Org is const.
std::vector::push_back() is a non-const method. So it can't be called in a context where the vector is const.
i'm trying to work with a class that uses a vector of smartpointer with reference counting that it had to implement on my own.
everything works fine but when i try to remove an iterator from my vector i get the following error:
(inside algorithm)
semantic issue
no viable overloaded '='
which occurs at:
template <class _InputIterator, class _OutputIterator>
inline _LIBCPP_INLINE_VISIBILITY
_OutputIterator
__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
{
for (; __first != __last; ++__first, ++__result)
*__result = _VSTD::move(*__first);
return __result;
}
which i guess is part of ...
here is my code:
#include "Project.h"
#include "Date.h"
#include "mSharedPtr.h"
class ProjectCycle
{
private:
mSmartPtr<Project> project;
Date startDate;
int numberOfEmployees;
std::vector<mSmartPtr<Employee>> employeeList;
public:
ProjectCycle(const mSmartPtr<Project>& pro, const Date& strdate, const int& numemp, const std::vector<mSmartPtr<Employee>>& list) :
project(pro), startDate(strdate), numberOfEmployees(numemp), employeeList(list) {};
void finishCycle();
~ProjectCycle();
void timeForwardHours(int);
void startProject();
void terminateProject();
void removeEmp(emp_vec::iterator);
};
cppFile:
#include "ProjectCycle.h"
#include <vector>
void ProjectCycle::timeForwardHours(int num) {
int totalDay=0;
// For each day ////////////////////////////////
for (int day=0; day<num; totalDay=0,day++) {
// For each employee ////////////////////////////
emp_vec::iterator empIter = employeeList.begin();
for (; empIter != employeeList.end(); empIter++) {
totalDay += (*empIter)->addHoursToCurrProj();
if ( (*empIter)->isDoneProject())
removeEmp(empIter);
}
/// Progress project ////////////////////////////////////
project->setHoursLeft( project->getHoursLeft()-totalDay );
if (project->getHoursLeft() <= 0) {
terminateProject();
break;
}
}
}
void ProjectCycle::removeEmp(emp_vec::iterator itr){
project->addToDoneList(itr);
employeeList.erase(itr);
thanks in advance,
I think the problem is caused by lack of a valid operator=() overload in mSmartPtr<Employee>.
Add the following member function
mSmartPtr& operator=( mSmartPtr&& rhs );
in mSmartPtr.
Problem
I want to assign values in a class definition which is in a separate header file from the class declaration cpp.
On compilation I recieve theses error messages:
error: ‘const std::map<unsigned int, std::basic_string<char> > bob::mRegexes’ is not a static member of ‘class bob’const std::map<uint,std::string> bob::mRegexes = {
^
error: ‘const std::map<unsigned int, std::basic_string<char> > bob::mResponses’ is not a static member of ‘class bob’ const std::map<uint,std::string> bob::mResponses = {
both of which have been absolutely infurriating because I do not understand why the compiler is ignoring thetypedef for std::string I feel like I'm missing something here but I'm not sure why the bob.h file is seeing the parameters differently than the bob.cpp.
bob.h
#ifndef BOB_H
#define BOB_H
#include <iostream>
#include <string>
#include <boost/regex.hpp>
#include <map>
typedef unsigned int uint;
// This was first to go when I started having problems.
/*using std::string;*/
using std::map;
// boost::regex > c++11::regex (gcc doesn't follow standards).
using boost::regex;
class bob
{
enum respond_to{
QUESTION,
YELL,
NAME,
DEFAULT,
LENGTH
};
public:
static const respond_to mResponseTypes;
static const map<uint,std::string> mRegexes;
static const map<uint,std::string> mResponses;
static std::string hey(std::string sentence);
static const std::string* evaluate (const std::string& sentence);
static const std::string* getResponse(const std::string& sentence, const respond_to& type) noexcept(true);
};
#endif
bob.cpp
#include "bob.h"
const std::map<uint,std::string> bob::mRegexes = {
{QUESTION, "[a-z]+\\?"},
{YELL,"[A-Z]+"}
};
const std::map<uint,std::string> bob::mResponses = {
{QUESTION,"Sure"},
{YELL,"Whoah, chill out!"},
{DEFAULT,"Whatever."}
};
// ...
This might be something really simple but I can't seem to work it out. Within my Vertex I have a std::list<Edge> but when I try to call methods on it like push_front I get an error saying the list is const and I can't push into it. I think for some reason the compiler is converting the std::list<Edge> to a const std::list<Edge>. I know my code isn't set up very well but it's just homework so I'm taking a few shortcuts.
Header file:
#ifndef GRAPH_H
#define GRAPH_H
#include <set>
#include <list>
class Edge{
public:
unsigned int to;
unsigned int weight;
};
class Vertex{
public:
unsigned int id;
std::list<Edge> edges;
bool operator<(const Vertex& other) const{
return id < other.id;
}
};
class Graph{
public:
void add_vertex(unsigned int id);
void add_edge(unsigned int from, unsigned int to, unsigned int weight);
std::set<Vertex> get_vertices();
std::list<Edge> get_edges(unsigned int id);
private:
std::set<Vertex> _vertices;
unsigned int size = 0;
};
Lines causing the error:
void Graph::add_edge(unsigned int from, unsigned int to, unsigned int weight)
{
Vertex find_vert;
find_vert.id = from;
set<Vertex>::iterator from_v = _vertices.find(find_vert);
Edge new_edge;
new_edge.to = to;
new_edge.weight = weight;
from_v->edges.push_front(new_edge); // ERROR HERE
}
Compiler Error message from running g++ -c Graph.cpp:
Graph.cpp:23:38: error: passing ‘const std::list<Edge>’ as ‘this’ argument of ‘void std::list<_Tp,
_Alloc>::push_front(const value_type&) [with _Tp = Edge; _Alloc = std::allocator<Edge>; std::list<_Tp,
_Alloc>::value_type = Edge]’ discards qualifiers [-fpermissive]
The contents of a std::set are implicitly const, because changing the contents could invalidate their sort order.
That makes from_v implicitly const here.
set<Vertex>::iterator from_v = _vertices.find(find_vert);
And your error is telling you that you're trying to modify a const object.
from_v->edges.push_front(new_edge);
// ^^^^^^ const ^^^^^^^^^^ non-const behavior