gpt4 book ai didi

c++ - 错误 : declaration does not declare anything

转载 作者:太空狗 更新时间:2023-10-29 23:25:36 32 4
gpt4 key购买 nike

这是一个非常业余的问题,我确信这将是一个非常简单的答案,但我似乎无法弄清楚问题所在。我有一个带有相应 .cpp 文件的头文件,但出于某种原因,每当我尝试使用 g++ 进行编译时,我都会收到错误消息:

declaration does not declare anything

我很确定问题是我没有初始化文件中的(唯一)变量,但我不确定将其初始化为什么。如果有人可以提供帮助,我将不胜感激!这是我的文件:

符号表字典.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

符号表词典.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;

编译错误:

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]

最佳答案

问题是这段代码:

// header
#ifndef SymbolTable
#define SymbolTable

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

你#define SymbolTable 为空。因此建议

  1. 始终对宏使用 ALL_UPPERCASE_NAMES(也用于包含守卫)

  2. 仅为宏使用宏名称。

关于c++ - 错误 : declaration does not declare anything,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15985542/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com