This Declaration Has No Storage Class or Type Specifier in C++

This declaration has no storage class or type specifier in C++

This is a mistake:

m.check(side);

That code has to go inside a function.
Your class definition can only contain declarations and functions.

Classes don't "run", they provide a blueprint for how to make an object.

The line Message m; means that an Orderbook will contain Message called m, if you later create an Orderbook.

How to fix derived class giving 'declaration has no storage class or type specifier' error?

I'm trying to initialize a base class variable in a derived class!

First of all, initialize the members in the base class Food's constructor, which you have provided.

Food(std::string name, int cost, int calories)
: name{ name }
, cost{ cost }
, calories{ calories }
{}

Then, you need to initialize the base class members in the constructor member initializer list of the derived class Bread:

class Bread : public Food 
{
public:
Bread()
:Food{ "", 5, 200 }
{}
};

which will initialize the Food members

name = ""
cost = 5
calories = 200

Declaration has no storage class or type specifier error when using stringstream

C++ does not allow executable statements outside functions.

The first two lines are declarations; they are allowed, although I doubt that you made them global on purpose. The last line, however, must be placed inside a function, e.g. main:

int main() {
int coins = 0;
std::stringstream ss;
ss << 100 << ' ' << 200;
}

declaration has no storage class or type specifier in C++

You need the standard library header <cstdlib> to use system.

Add

#include <cstdlib>

at the top of the file. Then, use std::system instead of just system.

std::system("pause");

Error: this declaration has no storage class or type specifier in c++

I'm gutting Engine to make the example smaller. We don't need any of the SDL stuff in the class to show how to fix this up. Other than that, Engine looks pretty good. Don't need to change anything.

namespace engine {

class Engine
{
private:

bool initialised = true;
bool quit = false;

public:

Engine(const char* , int , int ) // simplified to remove the SDL stuff
{
}

~Engine() // may want this to be virtual. See link about virtual destructors
{
}

void handleEvents()
{
}

virtual void update()
{

}

virtual void render()
{

}

void start()
{
if (initialised && !quit)
{
handleEvents();
update();
render();
}
}
};

}

Where things start going wrong is in trying to implement Engine. I'm going to comment inline as I make changes.

class Game : public engine::Engine // correct
{
// engine::Engine game{"engine", 1200, 600};
// don't need an engine::Engine as a member. Game IS an Engine.
public:
Game ():
engine::Engine {"engine", 1200, 600} // initialize base class here
{

}
// game.update() override
void update() override // use a normal function definition
// Compiler will figure out the rest
{

}

// game.render() override
void render() override // same as above
{

}
};

There's one small bug in main. The language has a little quirk where TYPE name() looks like a function, not a variable. This was cleaned up in C++11 with the addition of {} for more general initialization1.

int main()
{
// Game game(); declares a function, game, that returns a Game.
Game game; // defines a variable named game that is a Game. Could also be Game game{};
// note the different braces.
game.start();
return 0;
}

1 Watch out when using this with container classes. vector<int> test{5}; is a vector with one element containing five, NOT five elements containing the default zero you get from vector<int> test(5);.

Some additional reading:

When to use virtual destructors?

C++, What does the colon after a constructor mean?

The this declaration has no storage class or type specifier error in c++

You header file should only have declarations, something like this:

simplefactory.h:

#pragma once

// here you should also include the header files where your other classes (IAuto, Fiat etc) are declared

class SimpleAutoFactory {
public:
SharedInterfaces::IAuto *getAutoModel(EModel model); // only declaration, no definition
};

Then, in your cpp files you define your functions:

simplefactory.cpp:

#include "simplefactory.h"

SharedInterfaces::IAuto *SimpleAutoFactory::getAutoModel(EModel model) {
switch (model) {
case EModel::EFiat:
return new SharedModels::Fiat;

case EModel::ELamborghini:
return new SharedModels::Lamborghini;

case EModel::EMaserati:
return new SharedModels::Maserati;

default: throw CommonExceptions::unknownModelException;
}
}

Then you can either compile these files together with your main, or compile these files as a static or shared library and pass it to the linker.



Related Topics



Leave a reply



Submit