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

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

"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: &quot;no target&quot; in &quot;no project&quot; (compiler: unknown) ===|
[path]\beginner.cpp|    |In function &#39;int main()&#39;:|
[path]\beginner.cpp|2593|error: conflicting declaration &#39;const char* result&#39;|
[path]\beginner.cpp| 268|note: previous declaration as &#39;double result&#39;      |
[path]\beginner.cpp|2596|error: cannot convert &#39;double&#39; to &#39;const char*&#39;    |
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;|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Code:
#include &lt;iostream&gt;     /// These are Standard Library Feature ()
#include &lt;string&gt;
#include &lt;iomanip&gt;
#include &lt;ios&gt;
#include &lt;limits&gt;
#include &lt;cmath&gt;
#include &lt;vector&gt;
#include &lt;string.h&gt;
using namespace std;

int main(void)
{
     /// std::strchr : find first occurence

     // Find the first occurence of a character
     cout &lt;&lt; endl &lt;&lt; &quot;std::strchr : &quot; &lt;&lt; endl;

     // we use std::strchr to find all the characters one by one

    const char *str {&quot;Try not. Do, or do not. There is no try.&quot;};

    char target = &#39;T&#39;;
    const char *result = str;  // First Error occures here 
    size_t iterations{};

    while ((result = strchr(result, target)) != nullptr) {      // 
    And second error occurs here 
    cout &lt;&lt; &quot;Found &#39;&quot; &lt;&lt; target &lt;&lt; &quot; starting at &#39;&quot; &lt;&lt; &quot;\n&quot;;

    // Increment result, otherwise we&#39;ll find target at the same 
 location
        ++result;
        ++iterations;
    }

    cout &lt;&lt; &quot;iterations : &quot; &lt;&lt; iterations &lt;&lt; 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 &#39;const char* result&#39;
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:

确定