c++重定义错误
Redefinition Error C++
我在需要打印日期的部分遇到了麻烦。我试着做第二个变量,但它不工作。基本上,我会输入用户的生日。然后我尝试调用这个函数来确定出生日期(它确定一个代表一天的数字)。然后,我尝试将这个数字发送给函数,该函数接受该数字并以单词形式打印生日。我现在得到的错误是'int day2'重新定义。
下面是我的代码:void determineDayOfBirth() {
int day;
int month;
int year;
char backslash;
char backslash2;
cout << "Enter your date of birth" << endl;
cout << "format: month / day / year -->" << endl;
cin >> month >> backslash >> day >> backslash2 >> year;
if (isValidDate(month, day, year)) {
int day2;
cout << "You were born on a: ";
int day2 = determineDay(month, day, year);
printDayOfBirth(day2);
cout << endl;
cout << "Have a great birthday!!!";
}
else {
cout << "Invalid date";
}
return;
}
从第二个赋值中删除int
,你不能在同一个块中定义变量两次。
修改你的代码,替换:
int day2;
cout << "You were born on a: ";
int day2 = determineDay(month, day, year);
:
cout << "You were born on a: ";
int day2 = determineDay(month, day, year);
你放了两次"int day2",这确实是一个重新定义。你只需要定义一次"day2":
if (isValidDate(month, day, year)) {
int day2;
cout << "You were born on a: ";
day2 = determineDay(month, day, year); // REMOVE "int"
printDayOfBirth(day2);
cout << endl;
cout << "Have a great birthday!!!";
}
else {
cout << "Invalid date";
}
return;
问题的原因是
int day2;
cout << "You were born on a: ";
int day2 = determineDay(month, day, year);
第二个是day2
的重新定义。
从该行删除int
关键字,它将成为一个简单的赋值
你不能在同一个作用域中声明两个变量,所以day2在if块中声明了两次。你可以直接写:
//if(){
int day2 = determineDay(month, day, year);
//}
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 使用命名空间时出现多个定义错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 在运算符重载定义中使用成员函数(const错误)
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 已定义函数时出现 G++ "未定义的引用"错误
- C++ G++ 编译器 - 错误:隐式声明的定义
- 链接 cmake 时出现未定义的引用错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 链接阶段出现多重定义错误
- 已经以性格错误定义了
- vim ctag导致了对乐趣的错误定义
- C++ 隐式声明的编译器错误定义
- 编译器错误-定义问题.C++