error: expected type-specifier before 'Mobile' - c++

I did a research but solution is not matching to my program. From the large code i took only erroneous part. Please suggest.
#include <iostream>
using namespace std;
class Products {
static Products* prod;
public:
//Creat a static funtion which does the task what client was doing
static void checkStockStatus(int choice)
{
switch (choice) {
case 1:
prod = new Mobile();
break;
}
prod->inStockStatus();
}
virtual void inStockStatus() = 0;
};
class Mobile : public Products {
public:
void inStockStatus()
{
cout << "Mobiles are instock" << endl;
}
};
int main()
{
int choice = 1;
Products::checkStockStatus(choice);
return 0;
}
OutPut:
In static member function 'static void Products::checkStockStatus(int)':
14:29: error: expected type-specifier before 'Mobile'

Related

LNK error (Unresolved External symbol) when trying to use a struct in a class?

Hello I am getting a LNK error in the following code. I expect I am doing something wrong with the use of a class and struct together but can not find out what is wrong. I have seen people define structs outside a class then use instances of that struct in the class like I am trying to do here. I have also seen people use structs within the class itself but I thought in this case this made more sense.
The total error code is:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: static class std::vector<struct table,class std::allocator > tableOrder::TablesInUse" (?TablesInUse#tableOrder##2V?$vector#Utable##V?$allocator#Utable###std###std##A)
I don't really understand why what I am trying to do would obfuscate anything to do with tables in use from the compiler.
my main file is
#include"CashRegister.h"
#include"TableOrder.h"
#include<iostream>
#include "order.h"
using namespace std;
int main() {
tableOrder bigOrder;
cout << bigOrder.TablesInUse.size() << endl;
return 0;
}
TableOrder.h is
#pragma once
#include <string>
#include<vector>
using namespace std;
struct table {
unsigned int tableNumber;
unsigned int numChairs;
unsigned int numberOfPeople;
bool occupied;
//table();
void toggleOccupied() {
if (occupied == true) {
occupied = false;
}
else
{
occupied = true;
}
}
bool getTableOccupied() {
return occupied;
}
void setNumChairs(int newChairNum) {
numChairs = newChairNum;
}
void setTableNumber(int assignTableNum) {
tableNumber = assignTableNum;
}
int getTablenumber() {
return tableNumber;
}
int getNumberOfChairs() {
return numChairs;
}
void setNumberOfPeople(int addNumPeople) {
numberOfPeople = addNumPeople;
}
};
class tableOrder {
public:
tableOrder();
int getSP() {
return SP;
}
enum ServingProgress {
seated,
drinksOrder,
starters,
main,
dessert,
bill
};
ServingProgress SP = seated;
std::string progress;
void getCurrentProgress() {
switch (SP) {
case 0:
progress = "seated";
break;
case 1:
progress = "drinksOrder";
break;
case 2:
progress = "starters";
break;
case 3:
progress = "main";
break;
case 4:
progress = "dessert";
break;
case 5:
progress = "bill";
break;
}
}
void ProgressOrder() {
switch (SP) {
case 0:
SP = drinksOrder;
break;
case 1:
SP = starters;
break;
case 2:
SP = main;
break;
case 3:
SP = dessert;
break;
case 4:
SP = dessert;
break;
case 5:
SP = bill;
progress = "finished";
break;
}
}
void checkForTablesInUse() {
for (int i(0); i < allTables.size(); i++) {
if (allTables[i].occupied) {
TablesInUse.push_back(allTables[i]);
}
}
}
void checkForTablesNotInUse() {
for (int i(0); i < TablesInUse.size(); i++) {
if (TablesInUse[i].occupied == false) {
TablesInUse.erase(TablesInUse.begin() + i);
}
}
}
void updateTablesInUse() {
checkForTablesInUse();
checkForTablesNotInUse();
}
table& getTable(unsigned int tableIndex) {
return allTables[tableIndex - 1];
}
//instantiate tables
table table_1 = { 1,6,0,false };
table table_2 = { 2,5,0,false };
table table_3 = { 3,4,0,false };
table table_4 = { 4,8,0,false };
table table_5 = { 5,4,0,false };
//protected:
unsigned int assignedTableNumber;
table assignedTable;
static vector<table> availableTables;
static vector<table> TablesInUse;
static vector<table> TablesToSelect;
static vector<table> allTables;
};
and TableOrder.cpp is
#include "TableOrder.h"
tableOrder::tableOrder() {
assignedTableNumber = 0;
assignedTable = table_1;
vector<table> availableTables = { table_1 };
vector<table> TablesInUse = { table_1 };
vector<table> TablesToSelect = { table_1 };
vector<table> allTables = {table_1,table_2,table_3,table_4,table_5};
}
Thanks a lot for any help!
You've declared availableTables, TablesInUse, TablesToSelect and allTables as static members of tableOrder, but you don't define them. What you have done is define local variables in the tableOrder constructor.
You would need to add a few lines in TableOrder.cpp, at the global scope defining the variables, and then changing the code in the tableOrder constructor to assign the data you're trying to use to it; so something like:
vector<table> tableOrder::availableTables,
tableOrder::TablesInUse,
tableOrder::TablesToSelect,
tableOrder::allTables;
tableOrder::tableOrder() {
assignedTableNumber = 0;
assignedTable = table_1;
availableTables = { table_1 };
TablesInUse = { table_1 };
TablesToSelect = { table_1 };
allTables = {table_1,table_2,table_3,table_4,table_5};
}
Although, I don't quite get initializing the static members in the tableOrder constructor - is it supposed to be a singleton?
The Static Member tableOrder::TableInUse, although declared in TableOrder.h, was never defined.
The same applies to the 3 other static members of tableOrder.
In TableOrder.cpp, in tableOrder::tableOrder() you have 4 automatic variables named the same as the aforementioned static members. I think what you probably meant to do, which should also solve the problem, was:
#include "TableOrder.h"
std::vector tableOrder::availableTables;
std::vector tableOrder::TablesInUse;
std::vector tableOrder::TablesToSelect;
std::vector tableOrder::allTables;
tableOrder::tableOrder() {
...
availableTables = { table_1 };
TablesInUse = { table_1 };
TablesToSelect = { table_1 };
allTables = {table_1,table_2,table_3,table_4,table_5};
}

Fuction-definition not allowed RetailItem

I got some problem when run my coding. I got 2 separate file to create RetailItem class and create main. I create both in project.
Below are main.cpp
//main
#include "retailitem.h"
#include <iostream>
#include <iomanip>
using namespace std;
using std::cout;
void displayItem(RetailItem *, const int);
int main()
{
const int Item = 3;
RetailItem ritem[Item] ={ { "Jacket", 12, 59.95 },
{ "Designer Jeans", 40, 34.95 },
{ "Shirt", 20, 24.95 } };
//cout << fixed << setprecision(2);
void displayItem(RetailItem *ritem, const int Item){
cout <<" DESCRIPTION UNITS ON HAND PRICE";
cout<<"=================================================================\n";
for (int i = 0; i < Item; i++)
{
cout << setw(12) << ritem[i].getDesc();
cout << setw(12) << ritem[i].getUnits();
cout << setw(8) << ritem[i].getPrice();
}
cout << "===================================================================";
}
return 0;
}
and there one more file retailitem.h
//RetailItem class
#include <string>
using namespace std;
class RetailItem
{
private:
string description;
int unitsOnHand;
double price;
public:
RetailItem(string,int,double);
void setDesc(string d);
void setUnits(int u);
void setPrice(double p);
string getDesc();
int getUnits();
double getPrice();
};
RetailItem::RetailItem(string desc, int units, double cost)
{
description = desc;
unitsOnHand = units;
price = cost;
}
void RetailItem::setDesc(string d)
{
description = d;
}
void RetailItem::setUnits(int u)
{
unitsOnHand = u;
}
void RetailItem::setPrice(double p)
{
price = p;
}
string RetailItem::getDesc()
{
return description;
}
int RetailItem::getUnits()
{
return unitsOnHand;
}
double RetailItem::getPrice()
{
return price;
}
when compile and run main,
[Error] a function-definition is not allowed here before '{' token
[Error] expected '}' at end of input
I don't know what to fix, how can I solve it?
The error message undoubtedly contained a line number that told you where the problem was. That's an important part of describing the problem. But here it happens to be obvious: void displayItem(RetailItem *ritem, const int Item){ is the start of a function definition. You can't define a function inside another function. Move this outside of main.

C++ Class property not recognised by other class (headers incl., public) [duplicate]

This question already has answers here:
How to create two classes in C++ which use each other as data?
(2 answers)
Closed 6 years ago.
I have two classes each in Header files called "Map" and "Character". "Map" has a class called "map" and "Character" has a class called "character".
Class "map" inherits class "character", and both have the header file of the other included.
In one of the methods of class "map", I use a property member from class "character", and that works fine. However, when I try to use a "map" property member in class "character" it does not work. This happens regardless of whether or not "map" inherits "character".
This is class map which works fine:
#pragma once
#include <iostream>
#include "Character.h"
#include "AI.h"
using namespace std;
class map: public character
{
public:
static const int mapRow = 30; //-- all maps are the same size
static const int mapColumn = 116;
char marr[mapRow][mapColumn];
map()
{
for (int i = 0; i<mapRow; i++)
{
for (int j = 0; j<mapColumn; j++)
{
marr[i][j] = ' ';//set whole array to blank
}
}
}
void display(void);
void level1(void);
void level2(void);
void level3(void);
void treeBlueprint(int);
};
//displays the map
void map::display(void)
{
//This displays the level
for (int i = 0; i<mapRow; i++)
{
for (int j = 0; j<mapColumn; j++)
{
cout << marr[i][j];
}
cout << "\n";
}
}
This is class character that gives me the following errors when compiled:
'map':is not a class or namespace name
'map':is not a class or namespace name
'mapColumn': undeclared identifier
'mapRow': undeclared identifier
#pragma once
#include "Map.h"
#include <iostream>
using namespace std;
class character
{
int hpPool = 100;
int currentHp =hpPool;
public:
char Tommy ='?';
int position1;
int position2;
char movement;
character()
{
position1 = 5;
position2 = 5;
movement = '0';
}
void moveUp(void);
void moveDown(void);
void moveLeft(void);
void moveRight(void);
void moveFunct(void);
};
void character::moveUp(void)
{
if (position1 != 1)
{
position1--;
}
else
{
//Hitting a wall
}
}
void character::moveDown(void)
{
if (position1 != (map::mapRow-2))
{
position1++;
}
else
{
//Hitting a wall
}
}
void character::moveLeft(void)
{
if (position2 != 1)
{
position2--;
}
else
{
//Hitting a wall
}
}
void character::moveRight(void)
{
if (position2 != (map::mapColumn-2))
{
position2++;
}
else
{
//Hitting a wall
}
}
void character::moveFunct(void)
{
switch (movement)
{
case 'w': moveUp();
break;
case 'a': moveLeft();
break;
case 's': moveDown();
break;
case 'd': moveRight();
break;
}
}
Map.h is including Character.h and vice-versa, but that doesn't work (it would create infinite inclusion recursion if it weren't for the #pragma once).
Since character can not depend on map (because map is a derived class from character), it should not include Map.h.
My suggestions to improve your code base:
Remove using namespace std;.
map is already a class in the std namespace. It will be better to use a different name or use it under a namespace of your own.
namespace MyApp
{
class character { ... };
}
namespace MyApp
{
class map : public character { ... };
}

Why is the error "not declared in this scope" shows?

I am a beginner in C++. I am learning the topic friend functions. I have the code below in which two friend functions are declared in the class and called by the constructor but an error shows that the declared friend member functions are not declared in the scope. What am I doing wrong here? here is my code:
#include <iostream.h>
class Salary
{
private:
int sal[10];
public:
friend void add_details();
void display();
friend void display_des();
Salary()
{
add_details();
}
};
void add_details()
{
int loop = 0;
for(loop=0;loop<10;loop++)
{
cin >> sal[loop];
if (sal[loop] <= 0)
{
cout << "The amount should be greater than 0" << endl;
loop = loop - 1;
continue;
}
}
}
void display_des()
{
int sal_des[10];
int loop1 = 0, loop2;
for(loop1=0; loop1<10; loop1++)
{
sal_des[loop1] = sal[loop1];
}
for (loop1=0; loop1<10; loop1++)
{
for(loop2=loop1+1; loop2<10; loop2++)
{
if (sal_des[loop1]< sal_des[loop2])
{
int temp;
temp = sal_des[loop1];
sal_des[loop1] = sal_des[loop2];
sal_des[loop2] = temp;
}
}
}
for(loop1=0; loop1<10; loop1++)
{
cout << sal_des[loop1];
}
}
int main()
{
Salary sal1;
sal1.display_des();
return 0;
}
Also, another error inside function display_des() is shown as sal is not declared in this scope
You are defining a global function
void display_des();
instead of the member function
void Salary::display_des();
That means display_des has no "this"- Salary-object from which it could take the member sal[]. You also don't pass it a Salary-object from outside, so which sal[] should it use?
So either you use a friend function like this:
void display_des(Salary& obj){
obj.sal[...]...
}
Or you use a member-function, similar to this:
class Salary
{
private:
int sal[10];
public:
(...)
void display_des();
(...)
};
(...)
void Salary::display_des(){
sal[...]...
(...)
}

Policy based design - policy implementation has to access members of the host class

I think the best way to explain my question is with a piece of code:
class IncreasingMultiplier {
protected:
IncreasingMultiplier(int initialMultiplier = 0, int incrementation = 1)
int getMultiplier() {
mCurrentMultiplier += mIncrementation;
return mCurrentMultiplier - mIncrementation;
}
void setMultiplier(int multiplier) {
mCurrentMultiplier = multiplier;
}
void setIncrementation(int incrementation) {
mIncrementation = incrementation;
}
private:
int mCurrentMultiplier;
int mIncrementation;`
}
class ConstMultiplier {
protected:
int getMultiplier() const {
return 10;
}
}
class NumberLogger {
public:
void log() {
int currentNumber = getNumber(); // How can I call this method?
std::cout << "Current number is " << currentNumber << std::endl;
}
}
template<
class MultiplierPolicy,
class LoggingPolicy
>
class Host : public MultiplierPolicy, public LoggingPolicy {
public:
int getNumber() const {
return mNumber * getMultiplier();
}
private:
int mNumber;
}
Basically, one policy may need to access members defined in the host class, which are in turn dependent on other policies supplied to the host class.
Thanks!
The following code compiles with VS2013 (have not tried with GCC):
#include <iostream>
class IncreasingMultiplier {
protected:
IncreasingMultiplier(int initialMultiplier = 0, int incrementation = 1)
: mCurrentMultiplier(initialMultiplier)
, mIncrementation(incrementation)
{}
int getMultiplier() {
mCurrentMultiplier += mIncrementation;
return mCurrentMultiplier - mIncrementation;
}
void setMultiplier(int multiplier) {
mCurrentMultiplier = multiplier;
}
void setIncrementation(int incrementation) {
mIncrementation = incrementation;
}
private:
int mCurrentMultiplier;
int mIncrementation;
};
class ConstMultiplier {
protected:
int getMultiplier() const {
return 10;
}
};
// Template the logger policy
// Unfortunately - couldn't get host inheritance CRTP pattern
// compiling in Visual Studio 2013 :(
// https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
template < typename t_Host >
class NumberLogger /*: public t_Host*/ {
public:
void log() {
// This part of the CRTP pattern does work in Visual Studio 2013
int currentNumber = static_cast<t_Host*>(this)->getNumber(); // How can I call this method?
std::cout << "Current number is " << currentNumber << std::endl;
}
};
// Template based on a list of policies
template<
typename PoliciesList
>
class Host : public PoliciesList::MultiplierPolicy, public PoliciesList::LoggingPolicy {
public:
Host() : mNumber(1) {}
int getNumber() /*const*/ {
return mNumber * getMultiplier();
}
private:
int mNumber;
};
// Un-templated policies list
// Could create a macro to declare various policy combinations:
class ConcretePoliciesList_Const
{
public:
typedef Host<ConcretePoliciesList_Const> MyHost;
typedef ConstMultiplier MultiplierPolicy;
typedef NumberLogger<MyHost> LoggingPolicy;
};
class ConcretePoliciesList_Increasing
{
public:
typedef Host<ConcretePoliciesList_Increasing> MyHost;
typedef IncreasingMultiplier MultiplierPolicy;
typedef NumberLogger<MyHost> LoggingPolicy;
};
int main()
{
ConcretePoliciesList_Const::MyHost const_host;
ConcretePoliciesList_Increasing::MyHost increasing_host;
std::cout << "Const policy:" << std::endl;
const_host.log();
const_host.log();
const_host.log();
std::cout << "Increasing policy:" << std::endl;
increasing_host.log();
increasing_host.log();
increasing_host.log();
return 0;
}
The resulting output is:
Const policy:
Current number is 10
Current number is 10
Current number is 10
Increasing policy
Current number is 0
Current number is 1
Current number is 2