Error: declaration does not declare anything

24,677

Solution 1

The problem is this code:

// header
#ifndef SymbolTable
#define SymbolTable

// implementation
map<std::string, Symbol> SymbolTable;

You #define SymbolTable to empty. Therefore the advise to

  1. Always use ALL_UPPERCASE_NAMES for macros (also for include guards)

  2. Use macro names only for macros.

Solution 2

Instead of

#infndef SymbolTable
#define SymbolTable
..
#endif

You could just use

#pragma once

As far as I know, they do the same thing. But you don't have to deal with the name being the same as something else.

Solution 3

The name of your map SymbolTable is the same as macro used in include guard

#ifndef SymbolTable
#define SymbolTable

Because this macro is empty your declaration looks like this

map<std::string, Symbol> ; //note: no name

The solution is to use more ugly macro name for include guard, for example:

#ifndef SYMBOL_TABLE_H
#define SYMBOL_TABLE_H
Share:
24,677
Just Ask
Author by

Just Ask

Updated on May 02, 2020

Comments

  • Just Ask
    Just Ask about 4 years

    This is a very amateur question with what I'm sure is going to be a very simple answer, but I cannot seem to figure out the problem. I have a header file with a corresponding .cpp file, but for some reason whenever I try to compile with g++, I keep getting the error:

    declaration does not declare anything

    I'm pretty sure the problem is that I'm not initializing the (only) variable in the file, but I'm not sure what to initialize it to. If anyone can help, I would greatly appreciate it! Here are my files:

    SymbolTableDictionary.h

    #ifndef SymbolTable
    #define SymbolTable
    #include <new>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    #pragma once
    
    struct Symbol
    {
        std::string Name;
        int Address;
    
        Symbol::Symbol()
        { }
    
        Symbol::Symbol(const string name, int address)
        {
            std::string sym(name);
            this->Name = sym;
            this->Address = address;
        }
    };
    
    extern map<std::string, Symbol> SymbolTable;
    
    #endif
    

    SymbolTableDictionary.cpp

    #include <new>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <map>
    
    #include "SymbolTableDictionary.h"
    
    using namespace std;
    
    map<std::string, Symbol> SymbolTable;
    

    Compilation errors:

    In file included from SymbolTableDictionary.cpp:8:0:
    SymbolTableDictionary.h:18:5: error: extra qualification ‘Symbol::’ on member ‘Symbol’ [-fpermissive]
    SymbolTableDictionary.h:21:5: error: extra qualification ‘Symbol::’ on member ‘Symbol’ [-fpermissive]
    SymbolTableDictionary.h:29:8: error: declaration does not declare anything [-fpermissive]
    SymbolTableDictionary.cpp:12:1: error: declaration does not declare anything [-fpermissive]