. Advertisement .
..3..
. Advertisement .
..4..
I’m receiving this error: This declaration has no storage class or type specifier.
Here is my code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
class Message
{
public:
void check(string side)
{
if(side!="B"&&side!="S")
{
cout<<"Side should be either Buy (B) or Sell (S)"<<endl;;
}
}
};
class Orderbook
{
public:
string side;
Orderbook() //No Error if I define inside constructor
Message m; //No Error while declaring
m.check(side); //Error when I write m. or m->
};
These are two classes in my cpp. Aside from using an object after I create it, I have no issues. The code is too large to post. Why can’t I do everything in main but not in other Classes?
The cause: I think this line is a mistake:
Calling
m.check(side)
, it means that you are running actual code, but you can’t run code outsidemain()
– you can only define variables. Code in C++ is only permitted in function bodies and variable initializes.Solution: That code has to go inside a function. Only declarations and functions are permitted in your class definition.