“Conflicting Declaration” 和 “无法转换 …” 错误在 C++ 中发生。

huangapple go评论92阅读模式
英文:

"Conflicting Declaration" and "cannot convert ..." error in C++

问题

我正在尝试在Code::Blocks IDE中运行此代码,但我收到了两个错误消息:“冲突声明”和“无法转换...”。有人可以帮助我吗?
我在下面的源代码中指出了错误。

Code::Blocks中的构建消息

  1. ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
  2. [path]\beginner.cpp| |In function 'int main()':|
  3. [path]\beginner.cpp|2593|error: conflicting declaration 'const char* result'|
  4. [path]\beginner.cpp| 268|note: previous declaration as 'double result' |
  5. [path]\beginner.cpp|2596|error: cannot convert 'double' to 'const char*' |
  6. C:\Program Files\CodeBlocks\MinGW\x86_64-w64-mingw32\include\string.h|68|note: initializing argument 1 of 'char* strchr(const char*, int)'|
  7. ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

代码:

  1. #include <iostream> /// 这些是标准库特性(Standard Library Features)
  2. #include <string>
  3. #include <iomanip>
  4. #include <ios>
  5. #include <limits>
  6. #include <cmath>
  7. #include <vector>
  8. #include <string.h>
  9. using namespace std;
  10. int main(void)
  11. {
  12. /// std::strchr : 找到第一个出现的字符
  13. // 找到字符的第一个出现
  14. cout << endl << "std::strchr : " << endl;
  15. // 我们使用std::strchr逐个查找所有字符
  16. const char *str {"Try not. Do, or do not. There is no try."};
  17. char target = 'T';
  18. const char *result = str; // 第一个错误发生在这里
  19. size_t iterations{};
  20. while ((result = strchr(result, target)) != nullptr) { //
  21. 第二个错误发生在这里
  22. cout << "Found '" << target << "' starting at '";
  23. // 增加result,否则我们将在相同的位置找到目标
  24. ++result;
  25. ++iterations;
  26. }
  27. cout << "iterations : " << iterations << endl;
  28. }

谢谢你的帮助。

我在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

  1. ||=== Build file: &quot;no target&quot; in &quot;no project&quot; (compiler: unknown) ===|
  2. [path]\beginner.cpp| |In function &#39;int main()&#39;:|
  3. [path]\beginner.cpp|2593|error: conflicting declaration &#39;const char* result&#39;|
  4. [path]\beginner.cpp| 268|note: previous declaration as &#39;double result&#39; |
  5. [path]\beginner.cpp|2596|error: cannot convert &#39;double&#39; to &#39;const char*&#39; |
  6. C:\Program Files\CodeBlocks\MinGW\x86_64-w64-mingw32\include\string.h|68|note: initializing argument 1 of &#39;char* strchr(const char*, int)&#39;|
  7. ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
  1. Code:
  2. #include &lt;iostream&gt; /// These are Standard Library Feature ()
  3. #include &lt;string&gt;
  4. #include &lt;iomanip&gt;
  5. #include &lt;ios&gt;
  6. #include &lt;limits&gt;
  7. #include &lt;cmath&gt;
  8. #include &lt;vector&gt;
  9. #include &lt;string.h&gt;
  10. using namespace std;
  11. int main(void)
  12. {
  13. /// std::strchr : find first occurence
  14. // Find the first occurence of a character
  15. cout &lt;&lt; endl &lt;&lt; &quot;std::strchr : &quot; &lt;&lt; endl;
  16. // we use std::strchr to find all the characters one by one
  17. const char *str {&quot;Try not. Do, or do not. There is no try.&quot;};
  18. char target = &#39;T&#39;;
  19. const char *result = str; // First Error occures here
  20. size_t iterations{};
  21. while ((result = strchr(result, target)) != nullptr) { //
  22. And second error occurs here
  23. cout &lt;&lt; &quot;Found &#39;&quot; &lt;&lt; target &lt;&lt; &quot; starting at &#39;&quot; &lt;&lt; &quot;\n&quot;;
  24. // Increment result, otherwise we&#39;ll find target at the same
  25. location
  26. ++result;
  27. ++iterations;
  28. }
  29. cout &lt;&lt; &quot;iterations : &quot; &lt;&lt; iterations &lt;&lt; endl;
  30. }

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

错误消息:

  1. beginner.cpp|2593|错误:冲突声明 'const char* result'
  2. beginner.cpp|268|注:之前的声明为 'double result'

告诉您问题的具体内容和位置 — 您有两个冲突的 result 声明 — 第一个在 beginner.cpp 的第268行,第二个在第2593行。

英文:

The messages:

  1. beginner.cpp|2593|error: conflicting declaration &#39;const char* result&#39;
  2. beginner.cpp|268|note: previous declaration as &#39;double result&#39;

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

huangapple
  • 本文由 发表于 2023年7月23日 17:29:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定