c++ working with vector::erase - c++

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.

Related

Trying to do heapsort but got stuck

a new user at coding, trying to do heap sort but got stuck.
the error I am getting is:
`heap.cpp: In member function ‘void heap::Heapsort()’:
heap.cpp: error: no matching function for call to ‘heap::swap(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)’
swap(A[0],A[i]);
^
In file included from /usr/include/c++/8/vector:64,
from heap.cpp:2:
/usr/include/c++/8/bits/stl_vector.h:1367:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::swap(std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]’
swap(vector& __x) _GLIBCXX_NOEXCEPT
^~~~
/usr/include/c++/8/bits/stl_vector.h:1367:7: note: candidate expects 1 argument, 2 provided"
Please help!!I guess there is some error in the declaration of class. I am a complete noobie and this is my first course on data structures. It would be great if someone could help.I followed the heapsort code which is in cormen for most of it but the error seems to be prevalent.
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
class heap:public vector<int>{
private:
vector<int> &A;
int length;
int heap_size;
int P(int i){return (i-1)/2;}
int le(int i){return 2*i+1;}
int ri(int i){return 2*i+2;}
public:
void maxheap(int i);
void bmh(void);
void Heapsort(void);
heap(initializer_list<int> il):
vector<int>(il), A(*this),length(A.size()),heap_size(0) {}
heap(void):A(*this),length(A.size()),heap_size(0) {}// constructor
void show(void);
};
void heap::maxheap(int i)
{
int largest;
int l=le(i),r=ri(i);
if(l<=heap_size-1)&&A[l]>A[i])
largest=l;
else
largest=i;
if(r<=heap_size-1&&A[r]>A[i])
largest=r;
else
largest=i;
if(largest!=i){
swap(A[largest],A[i]);
maxheap(largest);
}
};
void heap::bmh(void)
{
heap_size=length;
for(int i=length/2;i>=0;i--)
{
maxheap(i);
}
}
void heap::Heapsort(void)
{
bmh();
for(int i=length-1;i>0;i--){
swap(A[0],A[i]);
heap_size=heap_size-1;
maxheap(i);
}
}
void heap::show(void)
{
for(int i=0;i<length-1;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
}
int main()
{
heap h<int>={16,4,10,14,7,9,3,2,8,1};
h.show();
//h.Build_Max_Heap();
//h.show();
// Test the member functions of heap class.
h.Heapsort();
h.show();
}
std::vector<int> has a member named swap. This member hides the global function std::swap. You can access it via its fully qualified name.
Besides, you haven't included either <algorithm> or <utility>, so it's possible std::swap isn't even declared.

How to create a custom channel in SystemC?

I am trying to create a custom channel in System C. Channel data structure is like following
struct Command {
int cmdType;
int lba;
double timestamp;
int size;
Command() { }
Command(const int c, const int l, const double ts, const int sz) {
make(c, l, ts, sz);
}
void make(const int c, const int l, const double ts, const int sz) {
cmdType = c;
lba = l;
timestamp = ts;
size = sz;
}
inline bool operator ==(const Command & command) const {
return (command.cmdType == cmdType && command.lba == lba
&& command.timestamp == timestamp
&& command.size == size); }
};
ostream and trace functions for this channel are defined as follows
inline ostream & operator <<(ostream & os, const Command command)
{
os << "CmdType " << command.cmdType << endl;
os << "Lba " << command.lba << endl;
os << "Time " << command.timestamp <<endl;
os << "Data " << command.size << endl;
return os;
}
inline void sc_trace(sc_trace_file * &tf, const Command & command, string &name)
{
sc_trace(tf, command.cmdType, name + ".cmdType");
sc_trace(tf, command.lba, name + ".lba");
sc_trace(tf, command.timestamp, name + ".timestamp");
sc_trace(tf, command.size, name + ".size");
}
however I am getting compile error
the error message is very long, however here is the last part
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:312:6: note: candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:317:6: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_signal_in_if<short int>&, const string&, int)
void sc_trace( sc_trace_file* tf,
^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:317:6: note: candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:322:6: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_signal_in_if<int>&, const string&, int)
void sc_trace( sc_trace_file* tf,
^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:322:6: note: candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:327:6: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_signal_in_if<long int>&, const string&, int)
void sc_trace( sc_trace_file* tf,
^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:327:6: note: candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:343:1: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const unsigned int&, const string&, const char**)
sc_trace( sc_trace_file* tf,
^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:343:1: note: candidate expects 4 arguments, 3 provided
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:351:13: note: void sc_core::sc_trace(sc_core::sc_trace_file*, const void*, const string&)
extern void sc_trace( sc_trace_file* tf,
^
/home/user/user/systemc-2.3.1//include/sysc/tracing/sc_trace.h:351:13: note: no known conversion for argument 2 from ‘const Command’ to ‘const void*’
In file included from /home/user/user/systemc-2.3.1//include/sysc/communication/sc_clock_ports.h:31:0,
from /home/user/user/systemc-2.3.1//include/systemc:79,
from /home/user/user/systemc-2.3.1//include/systemc.h:208,
from initiator.h:5,
from initiator.cpp:1:
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1808:1: note: template<class T> void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_inout<T>&, const string&)
sc_trace( sc_trace_file* tf, const sc_inout<T>& port,
^
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1808:1: note: template argument deduction/substitution failed:
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1165:46: note: ‘const Command’ is not derived from ‘const sc_core::sc_inout<T>’
sc_trace( p->tf, iface->read(), p->name );
^
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1791:1: note: template<class T> void sc_core::sc_trace(sc_core::sc_trace_file*, const sc_core::sc_in<T>&, const string&)
sc_trace(sc_trace_file* tf, const sc_in<T>& port, const std::string& name)
^
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1791:1: note: template argument deduction/substitution failed:
/home/user/user/systemc-2.3.1//include/sysc/communication/sc_signal_ports.h:1165:46: note: ‘const Command’ is not derived from ‘const sc_core::sc_in<T>’
sc_trace( p->tf, iface->read(), p->name );
^
appreciate help regarding how to fix this compile issue
here is simple version of the code --- file main.c
#include <stdio.h>
#include <csignal>
#include "systemc.h"
#include "producer.h"
#include "consumer.h"
#include "stdtype.h"
#include <iomanip>
#include <sstream>
using namespace std;
inline ostream & operator <<(ostream & os, const Command command)
{
os << "CmdType " << command.cmdType << endl;
return os;
}
inline void sc_trace(sc_trace_file * tf, const Command & command, const string & name)
{
int* cmdType = (int*) &(command.cmdType);
sc_trace(tf, cmdType, name + ".cmdType");
}
int sc_main(int arg_num, char *arg_vet[])
{
sc_clock clock("clock", 100, SC_PS);
sc_signal <bool> reset;
sc_signal <Command> cmd;
Producer *prd;
prd = new Producer("Producer");
prd->clock(clock);
prd->reset(reset);
prd->cmd_tx(cmd);
Consumer *con;
con = new Consumer("Consumer");
con->clock(clock);
con->reset(reset);
con->cmd_rx(cmd);
sc_trace_file *tf = NULL;
tf = sc_create_vcd_trace_file("trace");
sc_trace(tf, reset, "reset");
sc_trace(tf, clock, "clock");
reset.write(1);
sc_start(100, SC_NS);
reset.write(0);
sc_start(100, SC_NS);
sc_close_vcd_trace_file(tf);
return 0;
}
file consumer.h
#ifndef __CONSUMER_H__
#define __CONSUMER_H__
#include <queue>
#include <systemc.h>
#include "stdtype.h"
using namespace std;
SC_MODULE(Consumer)
{
sc_in_clk clock;
sc_in <bool> reset;
sc_in <Command> cmd_rx;
void ConsumerProcess();
SC_CTOR(Consumer) {
SC_METHOD(ConsumerProcess);
sensitive << reset;
sensitive << clock.pos();
}
};
#endif
file producer.h
#ifndef __PRODUCER_H__
#define __PRODUCER_H__
#include <queue>
#include <systemc.h>
#include "stdtype.h"
using namespace std;
SC_MODULE(Producer)
{
sc_in_clk clock;
sc_in <bool> reset;
sc_out <Command> cmd_tx;
void ProducerProcess();
SC_CTOR(Producer) {
SC_METHOD(ProducerProcess);
sensitive << reset;
sensitive << clock.pos();
}
};
#endif
file consumer.cpp
#include "consumer.h"
void Consumer:: ConsumerProcess(void)
{
cout << "con" << endl;
}
file producer.cpp
#include "producer.h"
void Producer:: ProducerProcess(void)
{
cout << "prd" << endl;
}
and file stdtype.h
#ifndef __STDTYPE_H__
#define __STDTYPE_H__
#include <queue>
#include <systemc.h>
struct Command {
int cmdType;
inline bool operator ==(const Command & command) const {
return (command.cmdType == cmdType); }
};
#endif
to compile above code here is the command line:
g++ -I. -I$SYSTEMC_HOME/include -L. -L$SYSTEMC_HOME/lib-linux64
-Wl,-rpath=$SYSTEMC_HOME/lib-linux64 -I/usr/local/include -L/usr/local/lib -lyaml-cpp -o main main.cpp producer.cpp consumer.cpp -lsystemc -lm
Are you sure that sc_trace function is right?
In order to be successful of your call, overloading function of two types is necessary at least. of course, I didn't think that implicit conversion class.
Anyway, two functions are as below:
sc_trace(sc_trace_file, int, string)
sc_trace(sc_trace_file, double, string)
`
inline void sc_trace(sc_trace_file * &tf, const Command & command, string &name)
{
sc_trace(tf, command.cmdType, name + ".cmdType"); // 1
sc_trace(tf, command.lba, name + ".lba"); // 1
sc_trace(tf, command.timestamp, name + ".timestamp"); // 2
sc_trace(tf, command.size, name + ".size"); // 1
}
found the fix!!!
changed stdtype.h as follows
#ifndef __STDTYPE_H__
#define __STDTYPE_H__
#include <queue>
#include <systemc.h>
#include <iomanip>
#include <sstream>
#include <map>
#include <utility>
#include <vector>
#include <string>
using namespace std;
struct Command {
int cmdType;
inline bool operator ==(const Command & command) const {
return (command.cmdType == cmdType); }
};
inline ostream & operator <<(ostream & os, const Command & command)
{
os << "CmdType " << command.cmdType << endl;
return os;
}
inline void sc_trace(sc_trace_file * tf, const Command & command, const string & name)
{
sc_trace(tf, command.cmdType, name + ".cmdType");
}
#endif
and removed these functions from main.cpp, this fixed the issue and i can trace the channel in vcd file, also added other code to communicate through the channel

C++ Template Class with Template Constructor

I tried to implement Properties in c++. I don't no why but if I want to compile my code there are quite a lot of errors. The main Idea was, that a template class and the tamplate constructor will give the requirement Informations.
I would be grateful if somebody could help me!
Compiling Message:
pi#raspberrypi ~/dev/property $ gcc -std=c++0x -o PropertyTest2 PropertyTest2.cpp
PropertyTest2.cpp:22:16: error: expected ‘;’ at end of member declaration
PropertyTest2.cpp:22:19: error: expected unqualified-id before ‘<’ token
PropertyTest2.cpp: In function ‘int main()’:
PropertyTest2.cpp:34:20: error: use of deleted function ‘PropertyTestClass::PropertyTestClass()’
PropertyTest2.cpp:8:7: error: ‘PropertyTestClass::PropertyTestClass()’ is implicitly deleted because the default definition would be ill-formed:
PropertyTest2.cpp:8:7: error: no matching function for call to ‘Property<int>::Property()’
PropertyTest2.cpp:8:7: note: candidates are:
Property4.cpp:21:2: note: template<int (** G)(), void (** S)(int&)> Property::Property()
Property4.cpp:6:7: note: constexpr Property<int>::Property(const Property<int>&)
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided
Property4.cpp:6:7: note: constexpr Property<int>::Property(Property<int>&&)
Property4.cpp:6:7: note: candidate expects 1 argument, 0 provided
PropertyTest2.cpp:38:20: error: no matching function for call to ‘Property<int>::Set(int)’
PropertyTest2.cpp:38:20: note: candidate is:
Property4.cpp:30:7: note: void Property<T>::Set(T&) [with T = int]
Property4.cpp:30:7: note: no known conversion for argument 1 from ‘int’ to ‘int&’
Property Class (Property.cpp)
#ifndef __PROPERTY_FH__
#define __PROPERTY_FH__
template <class T>
class Property {
private:
typedef T (*TGetter)(void);
typedef void (*TSetter)(T &);
TGetter Getter;
TSetter Setter;
public:
typedef T type;
template<TGetter *G,
TSetter *S
>
Property() {
this->Getter = G;
this->Setter = S;
}
T Get(void) {
return (this->Getter)();
}
void Set(T &value) {
(this->Setter)(value);
}
};
#endif
Testing file (PropertyTest.cpp):
#ifndef __PROPERTY_TEST_FH__
#define __PROPERTY_TEST_FH__
#include <iostream>
#include "Property.cpp"
class PropertyTestClass {
private:
// ReadWrite Property for age
int _age;
int AgeGetter(void) {
return this->_age;
}
void AgeSetter(int &value) {
this->_age = value;
}
public:
// ReadWrite Property for age
Property<int> age<&PropertyTestClass::AgeGetter, &PropertyTestClass::AgeSetter>;
};
#endif
/**
* Program Entry
**/
int main() {
std::cout << "Property Test Programm\n\n";
PropertyTestClass propTest;
std::cout << "ReadWrite Property for age\n";
propTest.age.Set(5);
std::cout << propTest.age.Get() << "\n";
return 0;
}
Ok, this time fixed all the problems in your code.
Property.cpp:
#ifndef __PROPERTY_FH__
#define __PROPERTY_FH__
#include <boost/function.hpp>
template <class T>
class Property {
private:
typedef boost::function <T()> TGetter;
typedef boost::function <void(const T&)> TSetter;
TGetter Getter;
TSetter Setter;
public:
typedef T type;
Property(TGetter G, TSetter S) {
this->Getter = G;
this->Setter = S;
}
T Get(void) {
return (this->Getter)();
}
void Set(const T &value) {
(this->Setter)(value);
}
};
#endif
PropertyTests.cpp:
#ifndef __PROPERTY_TEST_FH__
#define __PROPERTY_TEST_FH__
#include <iostream>
#include <boost/bind.hpp>
#include "Property.cpp"
class PropertyTestClass {
private:
// ReadWrite Property for age
int _age;
int AgeGetter() {
return this->_age;
}
void AgeSetter(const int &value) {
this->_age = value;
}
public:
// ReadWrite Property for age
Property<int> age;
PropertyTestClass() : age(
boost::bind(&PropertyTestClass::AgeGetter, this),
boost::bind(&PropertyTestClass::AgeSetter, this, _1))
{}
};
#endif
/**
* Program Entry
**/
int main() {
std::cout << "Property Test Programm\n\n";
PropertyTestClass propTest;
std::cout << "ReadWrite Property for age\n";
propTest.age.Set(5);
std::cout << propTest.age.Get() << "\n";
return 0;
}
Output:
$ ./a.out
Property Test Programm
ReadWrite Property for age
5

C++ compiler converting list to const list

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

Card and Deck: Problem linking classes (environment issues?)

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.