gpt4 book ai didi

c++ - 如何修复多重声明和多重初始化错误?

转载 作者:行者123 更新时间:2023-11-30 03:06:04 24 4
gpt4 key购买 nike

示例 1>

namespace Exercise {
int ivar = 0;
}

int ivar = 0;
using Exercise::ivar; // <<== error

错误 C2874:使用声明导致 'Exercise::ivar' 的多重声明

示例 2>

namespace Exercise {
double dvar = 0;
}

int main(int argc, char* argv[])
{
using Exercise::dvar;

double dvar = 3.1416; // <<== error
}

error C2374: 'Exercise::dvar' : redefinition; multiple initialization

我很难理解这些错误。有人可以给我一些细节吗?

最佳答案

namespace Exercise {
int ivar = 0; // create a new variable "::Exercise::ivar"
}
int ivar = 0; // create a new variable ::ivar;
using Exercise::ivar; // add name "::ivar" to variable "::Exercise::ivar", but that name is taken!

下一个例子:

namespace Exercise {
double dvar = 0; // create a new variable "::Exercise::dvar"
}
int _tmain(int argc, _TCHAR* argv[])
{
using Exercise::dvar; // add name "::_tmain(...)::dvar" to variable "::Exercise::dvar"
double dvar = 3.1416; // create a new variable "::_tmain(...)::dvar", but that name is taken!
}

关于c++ - 如何修复多重声明和多重初始化错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7045414/

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