I have this abstract class
class ImageIO {
protected:
ImageIO() {}
public:
virtual bool load(const std::string & filename, const std::string & format) = 0;
virtual bool save(const std::string & filename, const std::string & format) = 0;
}
And the implementation
class Image : public Array2D<Color>, public ImageIO {
private:
static int case_insensitive(string s) {
transform(s.begin(), s.end(), s.begin(), tolower);
if (s.compare("ppm") == 0) {
return 1;
}
return 0;
}
public:
bool load(const std::string& filename, const std::string& format) {
if (case_insensitive(format)) {
float* data = ReadPPM(filename.c_str(), (int*)width, (int*)height);
for (int i = 0, j = 0; i < width * height; i++, j += 3) {
buffer[i] = Color(data[j], data[j + 1], data[j + 2]);
}
delete[] data;
return true;
}
return false;
}
bool save(const std::string& filename, const std::string& format) {
if (case_insensitive(format)) {
float* data = new float[width * height * 3];
for (int i = 0, j = 0; i < width * height; i++, j += 3) {
data[j] = buffer[i].x;
data[j + 1] = buffer[i].y;
data[j + 2] = buffer[i].z;
}
bool write_check = WritePPM(data, width, height, filename.c_str());
delete[] data;
return write_check;
}
return false;
}
};
And I keep getting an abstract class cannot be instantiated error if I try this
Image test = Image();
in my main.
I've also tried declaring t=those two methods in a header file
class Image : public Array2D<Color>, public ImageIO {
private:
static int case_insensitive(std::string);
public:
bool ImageIO::load(const std::string& filename, const std::string& format);
bool ImageIO::save(const std::string& filename, const std::string& format);
};
but then I get unresolved external symbol errors. Is there a problem in the way I'm implementing the two functions or in how my declaring the class?
EDIT: If I declare those two methods in the header file then the error is:
error LNK2019: unresolved external symbol "public: virtual bool __thiscall image::Image::[image::ImageIO]::load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?load#?QImageIO#image##Image#2#UAE_NABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##0#Z) referenced in function _main
error LNK2019: unresolved external symbol "public: virtual bool __thiscall image::Image::[image::ImageIO]::save(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?save#?QImageIO#image##Image#2#UAE_NABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##0#Z) referenced in function _main
If I don't the error is
error C2259: 'image::Image': cannot instantiate abstract class
I also added override and nothing changes.
To specifically answer the question about unresolved external symbols, you don't need the ImageIO parent specifier on the save/load functions. You are declaring that Image itself has those functions, which override the abstract functions on the parent. You will then need to specify Image when implementing them in the cpp file.
Image.hpp
class Image : public Array2D<Color>, public ImageIO {
private:
static int case_insensitive(std::string);
public:
bool load(const std::string& filename, const std::string& format) override;
bool save(const std::string& filename, const std::string& format) override;
};
Image.cpp
bool Image::load(const std::string& filename, const std::string& format) {
if (case_insensitive(format)) {
//etc.
}
return false;
}
bool Image::save(const std::string& filename, const std::string& format) {
if (case_insensitive(format)) {
//etc.
}
return false;
}
My program compiles, but I get the following link errors:
1>desalter.obj : error LNK2020: unresolved token (0A000065) "private: static double (* DataBace::Number_d_save)[110]" (?Number_d_save#DataBace##$$Q0PAY0GO#NA)
1>desalter.obj : error LNK2020: unresolved token (0A000066) "private: static double (* DataBace::Flow_spec)[5]" (?Flow_spec#DataBace##$$Q0PAY04NA)
1>desalter.obj : error LNK2020: unresolved token (0A000068) "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * DataBace::Flow_Name" (?Flow_Name#DataBace##$$Q0PAV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A)
1>desalter.obj : error LNK2001: unresolved external symbol "private: static double (* DataBace::Flow_spec)[5]" (?Flow_spec#DataBace##$$Q0PAY04NA)
1>desalter.obj : error LNK2001: unresolved external symbol "private: static double (* DataBace::Number_d_save)[110]" (?Number_d_save#DataBace##$$Q0PAY0GO#NA)
1>desalter.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * DataBace::Flow_Name" (?Flow_Name#DataBace##$$Q0PAV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A)
1>C:\Users\Mojtaba Mohayeji\Documents\Visual Studio 2010\Projects\desalter\Debug\desalter.exe : fatal error LNK1120: 6 unresolved externals
Code:
class DataBace
{
public:
//---
void fLOW_NAME(int indicator, string flow_name)
{
Flow_Name[indicator] = flow_name;
}
string** fLOW_NAME_READ()
{
string** flow_name_read = new string*[1];
for (int i = 1; i < 100; i++)
{
string a = Flow_Name[i];
flow_name_read[i] = &a;
}
return flow_name_read;
}
int Show(string show)
{
for (int i = 0; i < 100; i++)
{
string sh = Flow_Name[i];
if (sh==show)
{
return i;
}
}
}
void FLOW_SPEC(int indicator, double flow_spec[1][5])
{
for (int i = 0; i < 5; i++)
{
Flow_spec[indicator][i] = flow_spec[1][i];
}
}
double** FLOW_SPEC_READ(int SHOW)
{
double** flow_spec_read = new double*[5];
for (int i = 1; i < 5; i++)
{
double a = Flow_spec[SHOW][i];
flow_spec_read[i] = &a;
}
return flow_spec_read;
}
void Number_d_SAVE(int indicator, double Number_d[110])
{
for (int i = 0; i < 110; i++)
{
Number_d_save[indicator][i] = Number_d[i];
}
}
double** Number_d_READ(int SHOW)
{
double** Number_d_read = new double*[110];
for (int i = 1; i < 110; i++)
{
double a = Number_d_save[SHOW][i];
Number_d_read[i] = &a;
}
return Number_d_read;
}
private:
static string Flow_Name[100];
static double Flow_spec[100][5];
static double Number_d_save[100][110];
};
I have written this code using SDL but there are some errors.
This is the code
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
#include <fstream>
#include <iostream>
using namespace std;
/////////////////////////////////// functions & global variable ////////////////////////////////
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Color textColor;
TTF_Font* font;
SDL_Surface* surface=SDL_GetWindowSurface(window);
SDL_Event event;
SDL_Texture* LoadTexture(char* path)
{
SDL_Surface* surface = IMG_Load(path);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer,surface);
SDL_FreeSurface(surface);
return texture;
}
///////////////////////////////////// Cell /////////////////////////////////////
class Cell
{
private:
char num;
public:
Cell();
Cell(int ,int ,char);
char * img;
pair <int ,int> cor;
int flag;
int size;
void display();
bool indomain(int ,int);
};
Cell :: Cell(int x ,int y ,char n){
num=n;
cor.first=x;
cor.second=y;
size=40;
flag=0;
if (num == '#')
img="0.png";
else if (num == '!')
img="1.png";
else
img="street.png";
}
bool Cell :: indomain(int x ,int y){
if ( (cor.first<=x && x<=cor.first+size) && (cor.second<=y && y<=cor.second+size))
return true;
return false;
}
void Cell :: display(){
SDL_Rect rect;
rect.x=cor.first;
rect.y=cor.second;
SDL_Texture* texture = LoadTexture(img);
SDL_RenderCopy(renderer,texture,NULL,&rect);
}
//////////////////////////////////////////// Tower /////////////////////////////////////////
class Tower
{
public:
Tower();
Tower(int ,int ,int ,int ,char* ,int ,int ,int);
~Tower();
int price;
int up_cost;
char * img;
int power;
int domain;
int grade;
int index;
pair <int ,int> cor;
void upgrade();
void display();
bool mark(int ,int);
};
Tower :: Tower(int pr ,int co ,int pow ,int dom ,char* im ,int x ,int y ,int i){
price=pr;
up_cost=co;
power=pow;
domain=dom;
img=im;
cor.first=x;
cor.second=y;
grade=1;
index=i;
}
void Tower :: upgrade(){
if (grade<5)
{
grade+=1;
power*=2;
domain*=1.2;
price*=1.2;
}
}
void Tower :: display(){
SDL_Rect rect;
rect.x=cor.first;
rect.y=cor.second;
SDL_Texture* texture = LoadTexture(img);
SDL_RenderCopy(renderer,texture,NULL,&rect);
}
bool Tower :: mark(int x, int y){
if ((cor.first<=x && x<=cor.first+40) && (cor.second<=y && x<=cor.second+80))
return true;
return false;
}
//////////////////////////////// Enemy //////////////////////////////////////
class Enemy
{
public:
Enemy();
Enemy(int ,char* ,int ,int ,int);
~Enemy();
char * img;
int health;
int cellNum;
int index;
pair <int ,int> cor;
bool live();
void display();
void damge(Tower t);
};
Enemy :: Enemy(int h ,char* im ,int x ,int y ,int i){
health=h;
img=im;
cor.first=x;
cor.second=y;
cellNum=0;
index=i;
}
bool Enemy :: live(){
if (health>0)
return true;
return false;
}
void Enemy :: damge(Tower t){
health-=t.power;
}
void Enemy :: display(){
SDL_Rect rect;
rect.x=cor.first;
rect.y=cor.second;
SDL_Texture* texture = LoadTexture(img);
SDL_RenderCopy(renderer,texture,NULL,&rect);
}
///////////////////////////////////////////// Map ///////////////////////////////////////
class Map
{
private:
char * file;
int len;
public:
Map();
Map(char* ,int);
~Map();
Cell map[15][20];
pair <int ,int> * path;
void display();
void put_tower(Tower ,Cell);
void put_enemy(Enemy ,Cell);
void remove(Cell);
void enemyMove(Enemy);
void pathFinder(int);
};
Map :: Map(char * f ,int l){
file=f;
len=l;
ifstream infile;
infile.open(file);
char * line=new char;
for (int i=0;i<15;i++)
{
infile >> line;
for (int j=0;j<20;j++)
map[i][j]=Cell(j*40 ,i*40 ,line[j]);
}
this->pathFinder(len);
}
void Map :: put_enemy(Enemy e ,Cell c){
e.cor=c.cor;
e.display();
}
void Map :: put_tower(Tower t ,Cell c){
t.cor=c.cor;
t.display();
}
void Map :: remove(Cell c){
c.display();
}
void Map :: enemyMove(Enemy e){
e.cellNum+=1;
e.cor=path[e.cellNum];
}
void Map :: pathFinder(int l){
path=new pair<int ,int> [l];
ifstream infile;
infile.open(file);
char ** lines=new char* [15];
for (int i=0;i<15;i++)
infile >> lines[i];
for (int i=0;i<20;i++)
if (lines[0][i]=='0')
{
path[0]=make_pair(0,i);
break;
}
int x=0;
int y=path[0].second;
int ord=48;
for (int i=1;i<l;i++)
{
if (int(lines[x-1][y])==((ord-47)%10)+48)
x-=1;
else if (int(lines[x+1][y])==((ord-47)%10)+48)
x+=1;
else if (int(lines[x][y-1])==((ord-47)%10)+48)
y-=1;
else if (int(lines[x][y+1])==((ord-47)%10)+48)
y+=1;
path[i]=make_pair(x,y);
ord+=1;
}
}
void Map :: display(){
for (int i=0;i<15;i++)
for (int j=0;j<20;j++)
map[i][j].display();
}
//////////////////////////////////////////////////// Game ////////////////////////////////////////
class Game
{
private:
int health;
int money;
int score;
int step;
public:
Game(int ,int);
~Game();
bool End;
int enemyNum;
int towerNum;
Enemy enemys[20];
Tower towers[50];
int getHealth();
int getMoney();
int getScore();
int getStep();
void setHealth(int);
void setMoney(int);
void setScore(int);
void setStep(int);
void distroy_enemy(Enemy ,Cell);
void distroy_tower(Tower ,Cell);
void shooting(Tower);
void drag_drop(Tower);
void upgradeTower(Tower);
void buyTower(Tower);
void sellTower(Tower ,Cell);
void display(Map);
void displayMenu();
void endDisplay();
bool checkEnd();
void animation();
};
Game :: Game(int h ,int m){
health=h;
money=m;
step=1;
score=0;
enemyNum=0;
towerNum=0;
End=false;
}
int Game :: getHealth(){
return health;
}
int Game :: getMoney(){
return money;
}
int Game :: getScore(){
return score;
}
int Game :: getStep(){
return step;
}
void Game :: setHealth(int h){
health=h;
}
void Game :: setMoney(int m){
money=m;
}
void Game :: setScore(int s){
score=s;
}
void Game :: setStep (int s){
step=s;
}
void Game :: distroy_enemy(Enemy e,Cell c){
for (int i=e.index;i<enemyNum-1;i++)
enemys[i]=enemys[i+1];
enemyNum-=1;
c.display();
}
void Game :: distroy_tower(Tower t ,Cell c){
for (int i=t.index;i<towerNum-1;i++)
towers[i]=towers[i+1];
towerNum-=1;
c.display();
}
void Game :: shooting(Tower t){
for(int i=0;i<enemyNum;i++)
{
if (sqrt(pow(enemys[i].cor.first,2)+pow(enemys[i].cor.second,2)) <= t.domain)
enemys[i].health-=t.power;
}
}
void Game :: upgradeTower(Tower t){
t.upgrade();
money-=t.up_cost;
}
void Game :: buyTower(Tower t){
money-=t.price;
towers[towerNum]=t;
towerNum+=1;
t.display();
}
void Game :: sellTower(Tower t ,Cell c){
distroy_tower(t,c);
money+=t.price/2;
}
void Game :: endDisplay(){
End=true;
SDL_FillRect(surface,&surface->clip_rect,SDL_MapRGB(surface->format,0XFF,0XFF,0XFF));
SDL_Surface* text;
font=TTF_OpenFont("AV.TTF",40);
textColor.b=20;
textColor.g=10;
textColor.r=0;
if (health<=0)
{
text=TTF_RenderText_Solid(font ,"You lose" ,textColor);
}
else if (health>0)
{
text=TTF_RenderText_Solid(font ,"Complete the mission" ,textColor);
}
SDL_Rect rect;
rect.x=300;
rect.y=300;
SDL_BlitSurface(text,NULL,surface,&rect);
}
bool Game :: checkEnd(){
if (health<=0 || step==20)
return true;
return false;
}
void Game :: displayMenu(){
SDL_Rect rect;
rect.x=800;
rect.y=0;
SDL_Texture* texture = LoadTexture("menu.png");
SDL_RenderCopy(renderer,texture,NULL,&rect);
}
void Game :: display(Map m){
m.display();
for (int i=0;i<towerNum;i++)
towers[i].display();
for (int i=0;i<enemyNum;i++)
enemys[i].display();
this->displayMenu();
/////////// ttf
}
void Game :: animation(){
SDL_Rect Lrect;
Lrect.x=83;
Lrect.y=0;
SDL_Rect Rrect;
Rrect.x=915;
Rrect.y=0;
SDL_Texture* leftpic = LoadTexture("left.png");
SDL_Texture* rightpic = LoadTexture("right.png");
for (int i=0;i<12;i++)
{
SDL_RenderCopy(renderer,leftpic,NULL,&Lrect);
SDL_RenderCopy(renderer,rightpic,NULL,&Rrect);
Rrect.x-=20;
Lrect.x+=60;
SDL_RendererFlip();
SDL_Delay(300);
}
}
////////////////////////////////////////////////////////////// main //////////////////////////////////////////////////////////////////////
int main()
{
window=SDL_CreateWindow("Frontline Defence",SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,940,600,SDL_WINDOW_SHOWN);
Game game(1000,500);
Map map("map.txt",30);
while ( !game.checkEnd() )
{
if ( SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
return 0;
if (event.type == SDL_MOUSEBUTTONDOWN )
{
SDL_Delay(3000);
}
}
}
game.endDisplay();
return 0;
}
I've done every thing I could but nothing changed!
I've tried including the libraries , typing all parts of the code in the main file and many other ways but it didn't help
These are the errors:
Error 12 error LNK1120: 8 unresolved externals C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\Debug\ConsoleApplication1.exe ConsoleApplication1
Error 4 error LNK2019: unresolved external symbol "public: __thiscall Cell::Cell(void)" (??0Cell##QAE#XZ) referenced in function "public: __thiscall Map::Map(char *,int)" (??0Map##QAE#PADH#Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1
Error 8 error LNK2019: unresolved external symbol "public: __thiscall Enemy::~Enemy(void)" (??1Enemy##QAE#XZ) referenced in function "public: __thiscall Game::Game(int,int)" (??0Game##QAE#HH#Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1
Error 7 error LNK2019: unresolved external symbol "public: __thiscall Enemy::Enemy(void)" (??0Enemy##QAE#XZ) referenced in function "public: __thiscall Game::Game(int,int)" (??0Game##QAE#HH#Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1
Error 10 error LNK2019: unresolved external symbol "public: __thiscall Game::~Game(void)" (??1Game##QAE#XZ) referenced in function "int __cdecl SDL_main(void)" (?SDL_main##YAHXZ) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1
Error 9 error LNK2019: unresolved external symbol "public: __thiscall Map::~Map(void)" (??1Map##QAE#XZ) referenced in function "int __cdecl SDL_main(void)" (?SDL_main##YAHXZ) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1
Error 6 error LNK2019: unresolved external symbol "public: __thiscall Tower::~Tower(void)" (??1Tower##QAE#XZ) referenced in function "public: __thiscall Game::Game(int,int)" (??0Game##QAE#HH#Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1
Error 5 error LNK2019: unresolved external symbol "public: __thiscall Tower::Tower(void)" (??0Tower##QAE#XZ) referenced in function "public: __thiscall Game::Game(int,int)" (??0Game##QAE#HH#Z) C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\classes.obj ConsoleApplication1
Error 11 error LNK2019: unresolved external symbol _SDL_main referenced in function _main C:\Users\FUJITSU\Desktop\frontline defence\project\ConsoleApplication1\ConsoleApplication1\SDL2main.lib(SDL_windows_main.obj) ConsoleApplication1
You're getting the linker errors referencing constructors and destructors because you haven't provided any implementation for those functions (as Michael Burr pointed out in a comment).
The unresolved external symbol _SDL_main referenced in function _main is because the mainfunction needs to have a specific signature: int main(int argc, char* argv[]) and check that you also have the SDL2main.libin the input to the linker.
Finally, it would seem that you never create any renderer (unless you create somewhere not included in your code example), so you have to add a call to SDL_CreateRenderer() after you create the SDL_Window in main.
I keep getting the same error messages every time on Visual Studio. I don't know where the error is originating. Basically I'm trying to convert an infix expression (A+B-C) to a postfix expression (AB+C-) using stacks. Any help would be greatly appreciated
DriverExpression.cpp
Expression.obj : error LNK2019: unresolved external symbol "public: __thiscall stackType::stackType(int)" (??0?$stackType#D##QAE#H#Z) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: __thiscall stackType::~stackType(void)" (??1?$stackType#D##QAE#XZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: char __thiscall stackType::top(void)const " (?top#?$stackType#D##QBEDXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: void __thiscall stackType::initializeStack(void)" (?initializeStack#?$stackType#D##QAEXXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: bool __thiscall stackType::empty(void)const " (?empty#?$stackType#D##QBE_NXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: void __thiscall stackType::push(char)" (?push#?$stackType#D##QAEXD#Z) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Expression.obj : error LNK2019: unresolved external symbol "public: void __thiscall stackType::pop(void)" (?pop#?$stackType#D##QAEXXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
C:\Users\Jake Bock\Desktop\CSC 240\Project 7\JBock_Project7\Debug\JBock_Project7.exe : fatal error LNK1120: 7 unresolved externals**
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include "Expression.h"
#include "stackType.h"
using namespace std;
int main()
{
string fileName;
string infixExpression, postfixExpression;
Expression expression;
cout << "Converting infix expression to postfix expression\n"
<< "testing infix expressions are in input file\n";
cout << "Enter the input file name ";
cin >> fileName;
ifstream inData;
ofstream outData;
inData.open(fileName.c_str());
outData.open("out.txt");
cout<<"open="<<fileName<<endl; //debugging print
while(inData>>infixExpression)
{
cout<<"Infix="<<infixExpression<<endl; //debugging print
expression.setInfix(infixExpression);
cout<<"setInfix="<<infixExpression<<endl;//debugging print
expression.convertInfixToPostfix();
cout<<"convert="<<infixExpression<<endl; //debugging print
postfixExpression = expression.getPostfix();
cout << "Infix Expression: "
<< infixExpression <<"\n"
<< "Postfix Expression: "
<<postfixExpression<<"\n\n";
outData << "Infix Expression: "
<< infixExpression <<"\n"
<< "Postfix Expression: "
<<postfixExpression<<"\n\n";
}
inData.close();
outData.close();
system("pause");
return 0;
}
================================================================
#ifndef EXPRESSION_H_
#define EXPRESSION_H_
#include "stackType.h"
class Expression
{
private:
std::string infixExpression;
std::string postfixExpression;
bool precedence(char s, char operator2); //ToDo
bool isOperand(char symbol); //ToDo
public:
Expression();
void setInfix(std::string infix);
std::string getInfix();
std::string getPostfix();
void convertInfixToPostfix(); //ToDo
static void convertInfixToPostfix( //ToDo
std::string& infix,
std::string& postfix);
virtual ~Expression();
};
#endif /* EXPRESSION_H_ */
#include <stack>
#include <string>
#include <sstream>
#include "Expression.h"
#include "stackType.h"
#include <iostream>
using namespace std;
Expression::Expression() : infixExpression(""), postfixExpression(""){}
void Expression::setInfix(string infix)
{
infixExpression= infix;
}
string Expression::getInfix()
{
return infixExpression;
}
string Expression::getPostfix()
{
return postfixExpression;
}
void Expression::convertInfixToPostfix()
{
/*Define operator stack*/
stackType<char> s(infixExpression.size());
s.initializeStack();
ostringstream strstream;
int operand = 0;
/*String stream will help us pushing integer to our stream easily.
This is similar to sprintf(), but with less hassles.*/
int size = infixExpression.size();
int iter = 0;
for (; iter < size; ++iter)
{
switch (infixExpression[iter])
{
case '+':
case '-':
case '/':
case '*':
{
if (s.empty())
{
/*We always push the first operator*/
s.push(infixExpression[iter]);
}
else
{
/*if precedence of current operator is higher than
one on top of the stack, we simply push the current
operator to the top of the stack*/
if (precedence(s.top(), infixExpression[iter]))
{
s.push(infixExpression[iter]);
}
else
{
/*Pop from the stack and append it to string stream.
Repeat it unless we find an operator on top of the stack
whose precedence is lower or stack is empty.*/
do
{
strstream << s.top() << ' ';
s.pop();
}while (!s.empty() && !precedence(s.top(), infixExpression[iter]));
/*Now push the current operator to the stack*/
s.push(infixExpression[iter]);
}
}
break;
}
case ' ':
{
break;
}
default:
{
while (iter < size && infixExpression[iter] >= '0' && infixExpression[iter] <= '9')
{
operand = operand * 10 + (infixExpression[iter]-'0');
++iter;
}
strstream << operand << ' ';
operand = 0;
--iter;
break;
}
}
}
/*Pop one by one from operator stack and append it to our stream*/
while(!s.empty())
{
strstream << s.top() << ' ';
s.pop();
}
/*Get the string associated with our string stream*/
postfixExpression = strstream.str();
}
bool Expression::precedence(char s, char operator_specified)
{
if ((operator_specified == '+') || (operator_specified == '-'))
return false;
char top_operator = s;
if ((top_operator == '+') || (top_operator == '-'))
return true;
else
return false;
}
Expression::~Expression()
{
}
//=======================================================================================
#ifndef STACKTYPE_H_
#define STACKTYPE_H_
template <class T>
class stackType
{
private:
int maxStackSize;
int stackTop;
char *list;
void copyStack(const stackType& otherStack);
public:
stackType();
stackType(int stackSize);
~stackType();
char top() const;
void initializeStack();
bool empty() const;
void push(const char newItem);
bool isFullStack() const;
void pop();
const stackType& operator=(const stackType& otherStack);
};
#endif /* stackType_H_ */
#include "stackType.h"
#include <stack>
#include <cassert>
template <class T>
stackType<T>::stackType(){}
template <class T>
stackType<T>::stackType(int stackSize)
{
if (stackSize <= 0)
{
cout << "Size of the array to hold the stack must be positive" << endl;
cout << "Creating an array of size 100" << endl;
maxStackSize = 100;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new char[maxStackSize];
}
template <class T>
stackType<T>::~stackType()
{
delete [] list;
}
template <class T>
char stackType<T>::top() const
{
assert(stackTop != 0);
return list[stackTop - 1];
}
template <class T>
void stackType<T>::initializeStack()
{
stackTop = 0;
}
template <class T>
bool stackType<T>::empty() const
{
return (stackTop == 0);
}
template <class T>
bool stackType<T>::isFullStack() const
{
return (stackTop == maxStackSize);
}
template <class T>
void stackType<T>::pop()
{
if (!isEmptyStack())
stackTop--;
else
cout << "Cannot remove from an empty stack" << endl;
}
template <class T>
void stackType<T>::push(const char newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cout << "Cannot add to a full stack" << endl;
}
template <class T>
void stackType<T>::copyStack(const stackType& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
}
template <class T>
const stackType<T>& stackType<T>::operator=(const stackType& otherStack)
{
if (this != &otherStack)
copyStack(otherStack);
return *this;
}
unresolved external symbol
Okay, so you've got a linker error here. Whenever you get that, here's the process for fixing it. First, let's look at the error message.
public: __thiscall stackType::stackType(int)" (??0?$stackType#D##QAE#H#Z) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix#Expression##QAEXXZ)
Figure out what method isn't linking. Looking at the error message, it's clearly stackType::stackType(int).
Now what's happening is your method (in this case, a constructor) is declared but never defined. So, go through the checklist...
Is stackType.cpp being included into your build?
Did you remember to write stackType::stackType(int) in your .cpp file?
Are the method signatures for the two functions the same? Did you make one a const method and the other not?
Any weird spelling errors in the class or method name?
Are you doing anything wrong involving templates?
My guess is that #4 is the culprit here, since stackType is a template class. You need to stick all those method definitions into the header and probably aren't.
So the problem turned out being that I needed to keep the template class member function definitions in the header file only. Oy.
I am working on an assignment for implementing queue functionality using only 2 stacks. I have the idea and the code down completely as far as I know but I am getting this error. From what I've looked at it is because there are multiple declarations for 1 variable that occur in the various .cpp files. I do not understand this error very well. My only guess is that I have a function for stack called "empty" and I have a similar function in queue by the same name. Could someone please explain to me where I am going wrong? Thanks for any help!
My code
EDIT:
Exact error message:
SQueueMain.obj : error LNK2005: "public: __thiscall Stack::Stack(void)" (??0Stack##QAE#XZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: __thiscall Stack::Stack(class Stack const &)" (??0Stack##QAE#ABV0##Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: __thiscall Stack::~Stack(void)" (??1Stack##QAE#XZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: class Stack const & __thiscall Stack::operator=(class Stack const &)" (??4Stack##QAEABV0#ABV0##Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: bool __thiscall Stack::empty(void)const " (?empty#Stack##QBE_NXZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall Stack::push(int const &)" (?push#Stack##QAEXABH#Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall Stack::display(class std::basic_ostream<char,struct std::char_traits<char> > &)const " (?display#Stack##QBEXAAV?$basic_ostream#DU?$char_traits#D#std###std###Z) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: int __thiscall Stack::top(void)const " (?top#Stack##QBEHXZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall Stack::pop(void)" (?pop#Stack##QAEXXZ) already defined in LStack.obj
SQueueMain.obj : error LNK2005: "public: __thiscall queue::queue(void)" (??0queue##QAE#XZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: __thiscall queue::~queue(void)" (??1queue##QAE#XZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall queue::enqueue(int)" (?enqueue#queue##QAEXH#Z) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: void __thiscall queue::dequeue(void)" (?dequeue#queue##QAEXXZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: int __thiscall queue::front(void)" (?front#queue##QAEHXZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "public: bool __thiscall queue::empty(void)" (?empty#queue##QAE_NXZ) already defined in SQueue.obj
SQueueMain.obj : error LNK2005: "private: class Stack::Node * __thiscall queue::setFront(void)" (?setFront#queue##AAEPAVNode#Stack##XZ) already defined in SQueue.obj
SQueueMain.cpp
//SQueueMain.cpp starts here---------------------------------------
#include "SQueue.cpp"
int main()
{
queue queue1;
int switchNum, tempNum;
while (1)
{
cout << "What action would you like to perform? \n";
cout << "Please enter the number that corresponds with the correct action. \n";
cout << "0: Enqueue an item. \n";
cout << "1: Dequeue an item. \n";
cout << "2: Return the front value. \n";
cout << "3: Determine if the queue is empty. \n";
cout << "4: Exit the program. \n";
cin >> switchNum;
switch (switchNum)
{
case 0:
{
cout << "What is the value you wish to enqueue? \n";
cin >> tempNum;
queue1.enqueue(tempNum);
}
break;
case 1:
{
queue1.dequeue();
}
break;
case 2:
{
cout << queue1.front();
}
break;
case 3:
{
cout << queue1.empty();
}
break;
case 4:
{
return 0;
}
}
}
}
//SQueueMain.cpp ends here-----------------------------------------
SQueue.cpp
//SQueue.cpp starts here---------------------------------------------------
#include "SQueue.h"
#include <new>
using namespace std;
queue::queue() : myFront(0) {}
queue::~queue()
{
queue::s1.~Stack();
queue::s2.~Stack();
}
void queue::enqueue(int num)
{
queue::s1.push(num);
}
void queue::dequeue()
{
queue::setFront();
queue::s2.pop();
}
queueElement queue::front()
{
queue::setFront();
if (!empty())
return (myFront->data);
else
{
cerr << "*** Queue is empty -- returing garbage ***\n";
return *(new queueElement);
}
}
bool queue::empty()
{
queue::setFront();
return (myFront == 0);
}
Stack::NodePointer queue::setFront()
{
int temp;
if (queue::s2.empty() == true)
{
while (queue::s1.empty() != true)
{
temp = queue::s1.myTop->data;
queue::s1.pop();
queue::s2.push(temp);
}
}
myFront = queue::s2.myTop;
return myFront;
}
//SQueue.cpp ends here---------------------------------------------------
SQueue.h
//SQueue.h starts here-------------------------------------------------------------
#include <iostream>
#include "LSTack.cpp"
using namespace std;
#ifndef SQUEUE
#define SQUEUE
typedef int queueElement;
class queue
{
public:
Stack s1, s2;
queue();
~queue();
void enqueue(int);
void dequeue();
queueElement front();
bool empty();
Stack::NodePointer myFront; //typedef from LStack.h
private:
Stack::NodePointer setFront();
};
#endif
//SQueue.h ends here--------------------------------------------------------------
LStack.cpp
//--- LStack.cpp -------------------------------------------------
#include <new>
using namespace std;
#include "LStack.h"
//--- Definition of Stack constructor
Stack::Stack(): myTop(0){}
//--- Definition of Stack copy constructor
Stack::Stack(const Stack & original)
{
myTop = 0;
if (!original.empty())
{
// Copy first node
myTop = new Stack::Node(original.top());
// Set pointers to run through the stack's linked lists
Stack::NodePointer lastPtr = myTop,
origPtr = original.myTop->next;
while (origPtr != 0)
{
lastPtr->next = new Stack::Node(origPtr->data);
lastPtr = lastPtr->next;
origPtr = origPtr->next;
}
}
}
//--- Definition of Stack destructor
Stack::~Stack()
{
// Set pointers to run through the stack
Stack::NodePointer currPtr = myTop, // node to be deallocated
nextPtr; // its successor
while (currPtr != 0)
{
nextPtr = currPtr->next;
delete currPtr;
currPtr = nextPtr;
}
}
//--- Definition of assignment operator
const Stack & Stack::operator=(const Stack & rightHandSide)
{
myTop = 0;
if (rightHandSide.empty()) return *this;
if (this != & rightHandSide) // check that not st = st
{
this->~Stack(); // destroy current linked list
// Copy first node
myTop = new Stack::Node(rightHandSide.top());
// Set pointers to run through the stacks' linked lists
Stack::NodePointer lastPtr = myTop,
rhsPtr = rightHandSide.myTop->next;
while (rhsPtr != 0)
{
lastPtr->next = new Stack::Node(rhsPtr->data);
lastPtr = lastPtr->next;
rhsPtr = rhsPtr->next;
}
}
return *this;
}
//--- Definition of empty()
bool Stack::empty() const
{
return (myTop == 0);
}
//--- Definition of push()
void Stack::push(const StackElement & value)
{
myTop = new Stack::Node(value, myTop);
}
//--- Definition of display()
void Stack::display(ostream & out) const
{
Stack::NodePointer ptr;
for (ptr = myTop; ptr != 0; ptr = ptr->next)
out << ptr->data << endl;
}
//--- Definition of top()
StackElement Stack::top() const
{
if (!empty())
return (myTop->data);
else
{
cerr << "*** Stack is empty "
" -- returning garbage ***\n";
return *(new StackElement); // "Garbage" value
}
}
//--- Definition of pop()
void Stack::pop()
{
if (!empty())
{
Stack::NodePointer ptr = myTop;
myTop = myTop->next;
delete ptr;
}
else
cerr << "*** Stack is empty -- can't remove a value ***\n";
}
LStack.h
#include <iostream>
using namespace std;
#ifndef LSTACK
#define LSTACK
typedef int StackElement;
class Stack
{
friend class queue;
public:
Stack();
Stack(const Stack & original);
~Stack();
const Stack & operator = (const Stack & rightHandSide);
bool empty() const;
void push(const StackElement & value);
void display(ostream & out) const;
StackElement top() const;
void pop();
/***** Function Members *****/
// Prototypes same as in preceding section
private:
/*** Node class ***/
class Node
{
public:
StackElement data;
Node * next;
//--- Node constructor
Node(StackElement value, Node * link = 0)
/*------------------------------------------------------
Precondition: value is received
Postcondition: A Node has been constructed with value
in its data part and itb next part set to link
(default 0).
------------------------------------------------------*/
{ data = value; next = link; }
};
typedef Node * NodePointer;
/***** Data Members *****/
NodePointer myTop; // pointer to top of stack
}; // end of class declaration
#endif
#include "SQueue.cpp" // KILL THIS or exclude SQueue.cpp in compilation process