英文:
"Conflicting Declaration" and "cannot convert ..." error in C++
问题
我正在尝试在Code::Blocks IDE中运行此代码,但我收到了两个错误消息:“冲突声明”和“无法转换...”。有人可以帮助我吗?
我在下面的源代码中指出了错误。
Code::Blocks中的构建消息
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
[path]\beginner.cpp| |In function 'int main()':|
[path]\beginner.cpp|2593|error: conflicting declaration 'const char* result'|
[path]\beginner.cpp| 268|note: previous declaration as 'double result' |
[path]\beginner.cpp|2596|error: cannot convert 'double' to 'const char*' |
C:\Program Files\CodeBlocks\MinGW\x86_64-w64-mingw32\include\string.h|68|note: initializing argument 1 of 'char* strchr(const char*, int)'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
代码:
#include <iostream> /// 这些是标准库特性(Standard Library Features)
#include <string>
#include <iomanip>
#include <ios>
#include <limits>
#include <cmath>
#include <vector>
#include <string.h>
using namespace std;
int main(void)
{
/// std::strchr : 找到第一个出现的字符
// 找到字符的第一个出现
cout << endl << "std::strchr : " << endl;
// 我们使用std::strchr逐个查找所有字符
const char *str {"Try not. Do, or do not. There is no try."};
char target = 'T';
const char *result = str; // 第一个错误发生在这里
size_t iterations{};
while ((result = strchr(result, target)) != nullptr) { //
第二个错误发生在这里
cout << "Found '" << target << "' starting at '";
// 增加result,否则我们将在相同的位置找到目标
++result;
++iterations;
}
cout << "iterations : " << iterations << endl;
}
谢谢你的帮助。
我在YouTube上学习C++。代码在教程视频中可以正常运行,但在我的环境中无法运行。教程视频中使用的IDE是VS Code,我的是Code::Blocks。我在一个文件中尝试所有的代码示例,因此在代码中写了预处理器。
英文:
I am trying this code in Code::Blocks IDE, but I receive the two errors "Conflicting Declaration" and "cannot convert ...". Can anyone help me please.
I indicate errors in the source code below.
Build messages in the Code::Blocks
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
[path]\beginner.cpp| |In function 'int main()':|
[path]\beginner.cpp|2593|error: conflicting declaration 'const char* result'|
[path]\beginner.cpp| 268|note: previous declaration as 'double result' |
[path]\beginner.cpp|2596|error: cannot convert 'double' to 'const char*' |
C:\Program Files\CodeBlocks\MinGW\x86_64-w64-mingw32\include\string.h|68|note: initializing argument 1 of 'char* strchr(const char*, int)'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Code:
#include <iostream> /// These are Standard Library Feature ()
#include <string>
#include <iomanip>
#include <ios>
#include <limits>
#include <cmath>
#include <vector>
#include <string.h>
using namespace std;
int main(void)
{
/// std::strchr : find first occurence
// Find the first occurence of a character
cout << endl << "std::strchr : " << endl;
// we use std::strchr to find all the characters one by one
const char *str {"Try not. Do, or do not. There is no try."};
char target = 'T';
const char *result = str; // First Error occures here
size_t iterations{};
while ((result = strchr(result, target)) != nullptr) { //
And second error occurs here
cout << "Found '" << target << " starting at '" << "\n";
// Increment result, otherwise we'll find target at the same
location
++result;
++iterations;
}
cout << "iterations : " << iterations << endl;
}
Thank you for your help.
I have learning C++ in the YouTube. The code works in the tutorial video but not in mine. The ide is VS code in the Tutorial video, mine is Code::Blocks. I am tring all code example in one file so I write preprocessors in the code.
答案1
得分: 1
错误消息:
beginner.cpp|2593|错误:冲突声明 'const char* result'
beginner.cpp|268|注:之前的声明为 'double result'
告诉您问题的具体内容和位置 — 您有两个冲突的 result
声明 — 第一个在 beginner.cpp 的第268行,第二个在第2593行。
英文:
The messages:
beginner.cpp|2593|error: conflicting declaration 'const char* result'
beginner.cpp|268|note: previous declaration as 'double result'
Tell you precisely what and where the problem is -- you have two declarations for result
that conflict -- the first one on line 268 and the second one on line 2593 of beginner.cpp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论