4

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]
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Just Ask
  • 329
  • 2
  • 6
  • 22
  • 2
    You should avoid `` and `` in most C++ programs, let alone `` and ``. Also, it is generally considered ill-advised to use `using namespace std;`, and it is considered ill-mannered to add it to a header. It would help if you identified the line where the compiler reports the problem. – Jonathan Leffler Apr 13 '13 at 07:52
  • I suggest using header guards with some variation on the theme of `#ifndef SYMBOLTABLEDICTIONARY_H_INCLUDED` (all caps, based on the name of the file, name unlikely to be used for other purposes). Another reasonable suggestion is to use a name based on the MD5 hash of the contents of some version of the file: `#ifndef H_779f06b8e4323b7991ddfd9078d79923` perhaps. – Jonathan Leffler Apr 13 '13 at 08:09
  • The fact that the header guard and variable name were the same was what caused the problem. Can't believe I did not see that before. Thanks for everyone's help! Much appreciated! – Just Ask Apr 13 '13 at 08:12
  • For reference, inside the `Symbol` struct, you don't need to call your constructor `Symbol::Symbol()`, etc. The errors about the extra `Symbol::` essentially tell you you're over-qualifying the name. Just call them `Symbol()` etc instead. – cHao Apr 13 '13 at 08:14
  • Thanks for the tip! Doing that is some weird habit of mine. Are there any problems that can occur from doing that? – Just Ask Apr 13 '13 at 08:25
  • A C++ compiler could refuse to compile your code; but apart from little things like that, ... – Jonathan Leffler Apr 13 '13 at 08:57

3 Answers3

11

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.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
5

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.

olevegard
  • 5,294
  • 1
  • 25
  • 29
  • 2
    @VasilyBiryukov Yes it does... What version are you running? How are you compiling? I use `#prgama once` for all my code, haven't had any issues with it using g++ – olevegard May 30 '13 at 18:02
  • You are right, g++ [supports](http://gcc.gnu.org/onlinedocs/gcc-4.4.0/cpp/Alternatives-to-Wrapper-_0023ifndef.html) `#prama once`. I've missed that. +1 to you. – Vasily Biryukov May 31 '13 at 15:15
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