I am trying to compile this project just to see how I am doing as I go along, but I still have a few more classes to implement that will inherit from Widget for different types. When I compile I keep getting C2678:
Error C2678 binary '==': no operator found which takes a left-hand
operand of type 'Widget' (or there is no acceptable conversion)
Assignment10 c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.11.25503\include\xutility line3107
I cant find where this error is thrown or why. Any help would be greatly appreciated. Sorry it's so sloppy, I've been messing with it a lot to try and find the problem.
#pragma once
#include <iostream>
using namespace std;
class Widget
{
public:
int idNumber;
//structors
Widget()
{
idNumber = 0;
}
Widget(int a)
{
idNumber = a;
}
~Widget();
//operations
int getId()
{
return idNumber;
}
//overload operators to compare idNumber
void operator= (Widget &rhs)
{
idNumber = rhs.idNumber;
}
};
----------------------------------------
#include "Widget.h"
#include <iostream>
using namespace std;
Widget::~Widget()
{
}
/*
bool Widget::operator< (Widget &rhs)
{
bool result;
result = idNumber < rhs.idNumber;
return result;
}*/
---------------------------------
#pragma once
#include "Widget.h"
#include <list>
#include <iostream>
using namespace std;
class Inventory
{
public:
Inventory();
Inventory(int i)
{
Widget w(i);
}
~Inventory();
//operations
//process order for shipment
void order(int widgID);
//recieve widget
void receive(int widgID);
//overlaod output operator
friend ostream& operator<<(ostream&, const Widget&);
protected:
list<Widget> onHand;
list<int> onOrder;
};
-------------------------------
#include "Inventory.h"
#include "Widget.h"
#include <iostream>
#include <list>
using namespace std;
Inventory::Inventory()
{
Widget w;
}
Inventory::~Inventory()
{
}
//recieve widget
void Inventory::receive(int widgID)
{
cout << " Recieved shipment of Widget type "
<< widgID << endl;
//find the iterator of the widget location
list<int>::iterator weNeed;
weNeed = find(onOrder.begin(), onOrder.end(), widgID);
//if end is returned, item not found
if (weNeed != onOrder.end())
{
cout << "Ship " << Widget(widgID) << " to fill back order" << endl;
onOrder.erase(weNeed);
}
else
{
onHand.push_front(Widget(widgID));
}
}
//process order
void Inventory::order(int widgID)
{
cout << " Recieved order for Widget type "
<< widgID << endl;
//find the iterator of the widget location
list<Widget>::iterator weHave;
weHave = find(onHand.begin(), onHand.end(), widgID);
//if end is returned, item not found
if (weHave != onHand.end())
{
cout << "Ship " << *weHave << endl;
onHand.erase(weHave);
}
else
{
cout << "Back order widget of type "
<< widgID << endl;
onHand.push_front(Widget(widgID));
}
}
ostream& operator<<(ostream& os, const Widget& wid)
{
os << wid.idNumber;
return os;
}
Related
I have a homework assignment in which I have to code some methods for a linked list and test with a driver from the professor. I keep running into this error:
no matching conversion for functional-style cast from 'int' to 'ItemType'
Here are my files for my "Node" class ItemType:
// ItemType.h.
#include <fstream>
const int MAX_ITEMS = 5;
enum RelationType {LESS, GREATER, EQUAL};
class ItemType{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print(std::ostream&) const;
void Initialize(int number);
private: int value;
};
And ItemType.cpp
#include <fstream>
#include <iostream>
#include "ItemType.h"
ItemType::ItemType()
{
value = 0;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::Initialize(int number)
{
value = number;
}
void ItemType::Print(std::ostream& out) const
// pre: out has been opened.
// post: value has been sent to the stream out.
{
out << value;
}
When I try to use the professors driver, I get an error with initializing the ItemType class with the constructor. I initialize them like so: classList.putItem(ItemType(4))
But I end up with the error stated above, I'm not sure where Im wrong, here is my driver:
#include "unsorted.h"
using namespace std;
int main() {
UnsortedType classList;
classList.PutItem(ItemType(4));
classList.PutItem(ItemType(5));
classList.PutItem(ItemType(4));
classList.PutItem(ItemType(4));
classList.PutItem(ItemType(8));
cout << "(original) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
classList.ShiftRight();
cout << "(shifted right) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
classList.DeleteItem(ItemType(4));
cout << "(delete all 4s) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
classList.ShiftRight();
cout << "(shift right) length: " << classList.GetLength() << endl; classList.ResetList();
classList.Print();
return 0;
}
You don't have a constructor for ItemType that takes an int. A simple fix would be to define that constructor:
ItemType(int v) : value{v} { }
I've recently begun learning C++ and I'm having some trouble updating a pointer in my Movie class. I've got a feeling I have an issue somewhere in my Move/Copy constructors but have been trying to solve this issue for hours, swapping pointers to references to values and finally coming here for help.
I have a class Movie, which contains a string name, string rating and int watched member variables. Another class Movies, holds a vector of the movies. On movie, my method increment_watched is supposed to increment the int watched by one, the value does get incremented but it seems like the value is not saved. The display_all_movies method in Movies, which simply displays the movies that it stores holds the old value for watched. I know the issue is probably something really small but I haven't been able to work out why the value isn't being saved.
If someone could explain why the pointer value seems to be getting overridden I'd appreciate it greatly. Thanks in advance!
Code is below, there is some debug couts in there.
Movie.h
#ifndef _MOVIE_H_
#define _MOVIE_H_
#include <string>
#include <iostream>
class Movie {
private:
std::string *name;
std::string *rating;
int *watched;
public:
std::string get_name();
std::string get_rating();
int get_watched();
void increment_watched();
Movie(std::string name, std::string rating, int watched_val); // normal constructor
Movie(const Movie &source); // copy constructor
Movie(Movie &&source); // move constructor
~Movie();
};
#endif // _MOVIE_H_
Movie.cpp
#include "Movie.h"
std::string Movie::get_name() {
return *name;
}
std::string Movie::get_rating() {
return *rating;
}
int Movie::get_watched() {
return *watched;
}
void Movie::increment_watched() {
std::cout << *watched << std::endl;
(*watched)++; // TODO FIX!
std::cout << *watched << std::endl;
}
Movie::Movie(std::string name, std::string rating, int watched_val)
: name{nullptr}, rating{nullptr}, watched{nullptr}{
std::cout << "Creating movie " << watched_val << " with normal constructor" << std::endl;
this->name = new std::string{name};
this->rating = new std::string{rating};
this->watched = new int{watched_val};
}
Movie::Movie(const Movie &source)
: Movie(*source.name, *source.rating, *source.watched) {
std::cout << "Creating movie " << *source.watched << " with copy constructor" << std::endl;
}
Movie::Movie(Movie &&source)
: Movie(*source.name, *source.rating, *source.watched) {
std::cout << "Creating movie " << *source.watched << " with move constructor" << std::endl;
source.name = nullptr;
source.rating = nullptr;
source.watched = nullptr;
}
Movie::~Movie() {
delete name;
delete rating;
delete watched;
}
Movies.h
#ifndef _MOVIES_H_
#define _MOVIES_H_
#include <vector>
#include <string>
#include <iostream>
#include "Movie.h"
class Movies {
private:
static int count;
std::vector<Movie> *movies;
public:
void add_movie(std::string &&name, std::string &&rating, int &&watch);
void increment_movie_count(std::string &&name);
void display_all_movies();
void count_movies();
Movies();
Movies(const Movie &source); // copy constructor
Movies(Movie &&source); // move constructor
~Movies();
};
#endif // _MOVIES_H_
Movies.cpp
#include "Movies.h"
int Movies::count = 0;
void Movies::add_movie(std::string &&name, std::string &&rating, int &&watch) {
bool contains {false};
for(auto movie : *movies) {
if(movie.get_name() == name) {
contains = true;
}
}
if(!contains) {
movies->push_back(Movie{name, rating, watch});
count++;
}
}
void Movies::increment_movie_count(std::string &&name) {
for(auto movie : *movies) {
if(movie.get_name() == name) {
movie.increment_watched();
}
}
}
void Movies::display_all_movies() {
for(auto movie : *movies) {
std::cout << "Title " << movie.get_name() << " Rating " << movie.get_rating() << " Watched " << movie.get_watched() << std::endl;
}
}
void Movies::count_movies() {
std::cout << "There are " << count << " movies " << std::endl;
}
Movies::Movies() {
movies = new std::vector<Movie>{};
}
Movies::~Movies() {
delete movies;
}
And finally main.cpp
#include <iostream>
#include "Movies.h"
using std::cout;
using std::endl;
int main() {
Movies *my_movies;
my_movies = new Movies();
(*my_movies).count_movies();
my_movies->add_movie("Big", "PG-13", 2);
my_movies->increment_movie_count("Big");
my_movies->display_all_movies();
return 0;
}
Changefor(auto movie : *movies) to for(auto& movie : *movies) to update the original movie objects. Otherwise, you're only updating copies of the movie objects.
I know this problem is assessed many times on these forums, but they really are unique to their specific cases most times.
This is a project for a class (on C++ no less), and the point of the project was to remake the classic board game Reversi.
I have toiled through code for hours and finally made a program that will work, or so I thought!
The big problem I am having seems to come from my deconstructor as it's giving me this error many of us have seen. My code is posted below and from my own debugging code (using helpful cout messages) I have determined that the program manages to run to the end of the the Game.cpp class. Only, it stumbles on the deconstructor and crashes before finally "ending nicely".
Board.h
#include <iostream>
#include <cstdlib>
#include <vector>
#ifndef BOARD_H
#define BOARD_H
using namespace std;
enum Piece {LIGHT, DARK, EMPTY, BORDER};
typedef int Move;
Move const NullMove = -1;
int const MAX_SQUARES = 100;
enum Direction {N=0, NE=1, E=2, SE=3, S=4, SW=5, W=6, NW=7};
class Board
{
public:
Board();
void reset();
void display();
void makeMove(Piece, Move);
bool isLegal(Piece, Move);
Piece getWinner();
Piece getPlayer();
void genMoves();
int numMoves();
Move getMove(int) const;
bool gameOver;
private:
Piece board[MAX_SQUARES];
int lightPieces;
int darkPieces;
vector<Move> goodMoves;
static Piece currentPlayer;
vector <int> offset;
};
#endif
Board.cpp
#include <iostream>
#include <cstdlib>
#include <vector>
#include "Board.h"
using namespace std;
Board::Board()
{
reset();
for(int i=0;i<MAX_SQUARES;++i)
{
if(i<11 || i>88 || i%10==0 || i%10==9)
board[i]=BORDER;
}
offset.push_back(10);
offset.push_back(11);
offset.push_back(1);
offset.push_back(-9);
offset.push_back(-10);
offset.push_back(-11);
offset.push_back(-1);
offset.push_back(9);
board[44] = LIGHT;
board[45] = DARK;
board[54] = DARK;
board[55] = LIGHT;
gameOver=false;
}
void Board::reset()
{
for(int i=0; i<MAX_SQUARES;++i)
board[i] = EMPTY;
}
void Board::display()
{
for(int i=0;i<MAX_SQUARES;++i)
{
switch(board[i])
{
case LIGHT:
cout << "|LG|";
break;
case DARK:
cout << "|DR|";
break;
case EMPTY:
cout << "| |";
break;
case BORDER:
if(i<9)
cout << "<0" << i << ">";
else if(i==9)
cout << "<09>\n----------------------------------------\n";
else if(i%10==9)
cout << "<$$>\n----------------------------------------\n";
else if(i%10==0)
cout << "<" << i << ">";
else if(i<11 || i>90)
cout << "<$$>";
break;
}
}
}
void Board::makeMove(Piece p, Move m)
{
genMoves(); //generate valid moves
cout << "generated moves\n";
int good = numMoves(); //gets number of moves
if(good>0) //if there are valid moves
{
cout << "more than 0\n";
for(int i=0;i<goodMoves.size();++i) //checking the valid move list
{
if(m==goodMoves[i]) //if our move is in the list
{
cout << "move was in list\n";
board[m]=p; //change square
if(board[m]==DARK)
cout << "ITS DARK\n";
else if(board[m]==LIGHT)
cout << "ITS LIGHT\n";
else if(board[m]==EMPTY)
cout << "ITS EMPTY WTF WTF WTF\n";
for(int i=0;i<8;++i) //checking directions
{
Piece opp =(p==LIGHT)? DARK : LIGHT; //Making an opposite piece
cout << "made opp\n";
int counter=0;
int toCheck = m+offset[i]; //making the next square to check
if(board[toCheck]==opp) //if it's the opposite colour from player
{
cout << "it was the opposite piece\n";
while(board[toCheck]!=BORDER && board[toCheck]!=EMPTY) //while it's a piece
{
cout << "there was a piece to check\n";
if(board[toCheck]==p && counter>0) //if it's player's piece and counter is higher than 0
{
cout << "this should flip stuff\n";
for(int k=m;k!=toCheck;k = k+offset[i])
{
board[k]=p;
cout << k;
}
break;
}
else
{
cout << "found nothing, keep trying..\n";
toCheck += offset[i]; //if not, step in direction
counter++;
}
}
}
}
}
cout << "move wasn't in list\n";
}
}
currentPlayer=(p==LIGHT)? DARK : LIGHT;
}
bool Board::isLegal(Piece p, Move m)
{
Piece opp =(p==LIGHT)? DARK : LIGHT; //Making an opposite piece
if(board[m]==EMPTY) //Checking that the space we're going is empty
{
for(int i=0;i<8;++i) //checking directions
{
int toCheck = m+offset[i]; //making the next square to check
if(board[toCheck]==opp) //if it's the opposite colour from player
{
while(board[toCheck]!=BORDER && board[toCheck]!=EMPTY) //while it's a piece
{
if(board[toCheck]==p) //if it's player's piece
return true; // if move is valid
else
toCheck += offset[i]; //if not, step in direction
}
}
}
return false; // if there's no valid direction moves
}
else // if it's not empty
return false;
}
Piece Board::getWinner()
{
bool gameDone = true;
for(int i=0;i<MAX_SQUARES;++i)
{
if(board[i]==EMPTY)
gameDone = false;
}
if(gameDone==false)
return EMPTY;
else if(lightPieces>darkPieces)
return LIGHT;
else
return DARK;
}
Piece Board::getPlayer()
{
return currentPlayer;
}
void Board::genMoves()
{
goodMoves.clear();
cout << "generating shit\n";
for(int i=0;i<MAX_SQUARES;++i)
{
if(isLegal(currentPlayer, i))
{goodMoves.push_back(i);
cout << i << " twas a good move\n";}
}
if(goodMoves.size()==0)
gameOver=true;
}
int Board::numMoves()
{
return goodMoves.size();
}
Move Board::getMove(int i) const
{
return goodMoves[i];
}
Piece Board::currentPlayer=DARK;
Player.h
#include <iostream>
#include <cstdlib>
#ifndef PLAYER_H
#define PLAYER_H
#include "Board.h"
using namespace std;
class Player
{
public:
Player(const string&, Piece);
Piece getPiece() const;
virtual void makeMove(Board&)=0;
void setName(string&);
string getName();
private:
string name;
Piece color;
};
#endif
Player.cpp
#include <iostream>
#include <cstdlib>
#include "Player.h"
using namespace std;
Player::Player(const string& n, Piece c)
{
name = n;
color = c;
}
Piece Player::getPiece() const
{
return color;
}
void Player::setName(string& n)
{
name = n;
}
string Player::getName()
{
return name;
}
HumanPlayer.h
#include <iostream>
#include <cstdlib>
#include "Player.h"
#ifndef HUMANPLAYER_H
#define HUMANPLAYER_H
using namespace std;
class HumanPlayer: public Player
{
public:
HumanPlayer(const string&, Piece);
void makeMove(Board&);
};
#endif
HumanPlayer.cpp
#include <iostream>
#include <cstdlib>
#include "Player.h"
#include "HumanPlayer.h"
using namespace std;
HumanPlayer::HumanPlayer(const string& n, Piece c): Player(n,c)
{
}
void HumanPlayer::makeMove(Board& b)
{
Move goTo;
cout << "Please enter the number for the square you would like to move: ";
cin >> goTo;
if(!b.gameOver)
b.makeMove(getPiece(),goTo);
}
ComputerPlayer.h
#include <iostream>
#include <cstdlib>
#include "Player.h"
#ifndef COMPUTERPLAYER_H
#define COMPUTERPLAYER_H
using namespace std;
class ComputerPlayer: public Player
{
public:
ComputerPlayer(Piece p);
private:
static int counter;
//string name;
};
#endif
ComputerPlayer.cpp
#include <iostream>
#include <cstdlib>
#include "ComputerPlayer.h"
using namespace std;
ComputerPlayer::ComputerPlayer(Piece p) : Player("", p)
{
string name = "ComputerPlayer" + char(65+counter);
setName(name);
}
int ComputerPlayer::counter=0;
RandomPlayer.h
#include <iostream>
#include <cstdlib>
#include "ComputerPlayer.h"
#ifndef RANDOMPLAYER_H
#define RANDOMPLAYER_H
using namespace std;
class RandomPlayer : public ComputerPlayer
{
public:
RandomPlayer(Piece);
void makeMove(Board&);
};
#endif
RandomPlayer.cpp
#include <iostream>
#include <cstdlib>
#include "RandomPlayer.h"
using namespace std;
RandomPlayer::RandomPlayer(Piece p) : ComputerPlayer(p)
{
}
void RandomPlayer::makeMove(Board& b)
{
cout << "Player 2 making move in stuff\n";
b.genMoves();
int temp1 = b.numMoves();
cout << "This is temp1: " <<temp1 << '\n';
int temp2;
if(temp1>0)
{
temp2 = rand()%temp1;
}
//cout << "This is temp2: " <<temp2 << '\n';
if(!b.gameOver)
b.makeMove(getPiece(),b.getMove(temp2));
}
Game.h
// Name: James St-Germain
// Student #: 0270250
#include <iostream>
#include <cstdlib>
#include "Board.h";
#include "HumanPlayer.h"
#include "RandomPlayer.h"
#ifndef GAME_H
#define GAME_H
using namespace std;
class Game
{
public:
Game();
~Game();
void selectPlayers();
Player* nextPlayer();
void play();
void announceWinner();
private:
Board b;
Player *p1;
Player *p2;
bool isRunning;
};
#endif
Game.cpp
// Name: James St-Germain
// Student #: 0270250
#include "Game.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
Game::Game(): b(), p1(NULL), p2(NULL), isRunning(true){}
Game::~Game()
{
delete &b;
delete &p1;
delete &p2;
}
void Game::selectPlayers()
{
string choice[2];
cout << "Is player 1 a human player or computer player? (H/C): \n";
cin >> choice[0];
cout << "Is player 2 a human player or computer player? (H/C): \n";
cin >> choice[1];
for(int i=0;i<2;++i)
{
if(choice[i]=="H")
{
string n;
char* c;
cout << "What is your name?: \n";
cin >> n;
if(i==0)
p1 = new HumanPlayer(n, LIGHT);
else
p2 = new HumanPlayer(n, DARK);
}
if(choice[i]=="C")
{
if(i==0)
p1 = new RandomPlayer(LIGHT);
else
p2 = new RandomPlayer(DARK);
}
}
cout << "Player 1 is " << p1->getName() << '\n';
cout << "Player 2 is " << p2->getName() << '\n';
}
Player* Game::nextPlayer()
{
if(b.getPlayer()==LIGHT)
return p2;
else
return p1;
}
void Game::play()
{
while(isRunning)
{
b.display();
Piece temp = b.getPlayer();
if(temp==LIGHT)
{
cout << "Player 1 moves!\n";
p1->makeMove(b);
}
else
{
cout << "Player 2 moves!\n";
p2->makeMove(b);
}
if(b.gameOver==true)
break;
}
}
void Game::announceWinner()
{
Piece winner = b.getWinner();
string name = (winner==LIGHT) ? p1->getName() : p2->getName();
cout << "The winner is " << name << "! Congratulations!\n";
}
main.cpp
#include <iostream>
#include <cstdlib>
#include "Game.h"
using namespace std;
int main()
{
Game Reversi = Game();
Reversi.selectPlayers();
Reversi.play();
Reversi.announceWinner();
}
I apologize for the extreme amount of code, but at this point, I don't know what to fix. I know there might also be bad coding habits here, so if you see any, I would love the constructive criticism.
Thank you in advance for all your help!!
This is probably because you have these declarations in the Game class:
Board b;
Player *p1;
Player *p2;
and this code in the destructor:
delete &b;
delete &p1;
delete &p2;
First of all, the Board member b is not a pointer so should not be deleted. Second of all, you're using the address-of operator to get the address of the pointer (and it will be a value of type Player**), which you don't allocate. Drop the &.
I am writing a program in VC++. Here I am declaring class Product and Client.In client I'm using a function list initProduct() in which list::iterator i; is used.I'm unable to display list using iterator.
This my code:
#include "StdAfx.h"
#include <iostream>
#include <string>
#include <list>
#include <iterator>
using namespace std;
class Product
{
int item_code;
string name;
float price;
int count;
public:
void get_detail()
{
cout<<"Enter the details(code,name,price,count)\n"<<endl;
cin>>item_code>>name>>price>>count;
}
};
class Client
{
public:
list<Product> initProduct()
{
char ans='y';
list<Product>l;
list<Product>::iterator i;
while(ans=='y')
{
Product *p = new Product();
p->get_detail();
l.push_back(*p);
cout<<"wanna continue(y/n)"<<endl;
cin>>ans;
}
cout<<"*******"<<endl;
for(i=l.begin(); i!=l.end(); i++)
cout << *i << ' '; //ERROR no operator << match these operand
return l;
}
};
int main()
{
Client c;
c.initProduct();
system("PAUSE");
}
You must implement the following function
class Product {
// ...
friend std::ostream& operator << (std::ostream& output, const Product& product)
{
// Just an example of what you can output
output << product.item_code << ' ' << product.name << ' ';
output << product.price << ' ' << product.count;
return output;
}
// ...
};
You declare the function a friend of the class because it must be able to access the private properties of a Product.
You need to produce an ostream& operator<<(ostream& os, const Product& product) that prints the information you want to display.
If you're using C++11 you can use auto :
for(auto it : Product)
{
cout << it.toString();
}
but you'll have to implement this toString() which will display all the infos you want
I have a base class called Item:
#ifndef ITEM_H
#define ITEM_H
#include <ostream>
class Item {
public:
virtual ~Item() {}
virtual void print(std::ostream& out) const {}
friend std::ostream& operator << (std::ostream& out, Item& item){
item.print(out);
return out;
}
};
#endif
and I have a derived class Tower:
#ifndef TOWER_H
#define TOWER_H
#include <iostream>
#include <iostream>
#include <exception>
#include "item.h"
#include "Card.h"
class Tower : public Item {
unsigned height;
void print(std::ostream& o) const;
public:
Tower(const Card& card);
int demolish(Card& card) const;
unsigned getHeight() const;
};
#endif
Source code for Tower:
#include "tower.h"
Tower::Tower(const Card& card){
height = card.getDis();
}
void Tower::print(std::ostream& o) const {
o << height;
}
int Tower::demolish(Card& card) const{
try {
if(height != card.getDis()){
throw std::exception ();
} else {
return height;
}
} catch (std::exception e){
cout<< "Card's value not enough to demolish the tower." << endl;
}
}
unsigned Tower::getHeight() const {
return height;
}
Now I'm trying to test the code to see if the operator overloading works properly:
void test() {
Card card (Card::SOUTH, 3);
Tower tower(card);
std::cout << "Printing tower: " << tower << std::endl; //PRINTS OUT 3 AS EXPECTED
Card one (Card::NORTH, 2);
Card two (Card::WEST, 3);
std::cout << "Expecting to receive an error: " <<endl;
tower.demolish(one);
std::cout << "Expecting to have the tower demolished: ";
std::cout << tower.demolish(two) <<std::endl;
std::cout << "Height of the tower: " << tower.getHeight() <<std::endl;
std::vector<Item> items; //creating an Item vector
items.push_back(Tower(one));
Item items2[1]; //creating an array of Items
items[0]= tower;
std::cout << "Printing out an Item: ";
std::cout << items.back()<<std::endl; //EXPECTING TO GET 2 BUT IT PRINTS NOTHING, WHY IS THAT?
std::cout << "OR: " << items2[0]<<std::endl; //SAME ISSUE HERE, EXPECTING TO GET 3
}
As can be understood from the code, a Card holds an integer value distance and an enum value direction. It would've been a mess if i included that code too. I have commented my questions in the last piece of code test(). Thanks for your help in advance.
std::vector<Item> items; //creating an Item vector
items.push_back(Tower(one));
What happens here is called "slicing". Since you're not storing pointers, but actual objects, the Tower part of the class is just cut off and only the Item part is pushed into the vector. To use virtual functions and polymorphism, you need a reference or pointer to the base class.
std::vector<Item*> items; //creating an Item vector
items.push_back(new Tower(one));
// ...
// at the end of main:
for(int i=0; i < items.size(); ++i)
delete items[i];
Or with smart pointers from Boost or a C++11 library
std::vector<shared_ptr<Item>> items;
items.push_back(make_shared<Tower>(one));
// nothing further needs to be done
For printing, you now obviously need to dereference the pointer:
std::cout << "Printing out an Item: ";
std::cout << *items.back()<<std::endl;
std::cout << "OR: " << *items2[0]<<std::endl;
}