-3

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.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 1
    Please provide your entire code, without removing includes and `main()`. Also copypaste the entire error from the "build log" tab. – HolyBlackCat Jul 23 '23 at 09:32
  • Please see [mre]. It is beneficial to your question and your education to remove as much code from your example as you can while retaining the error (and retaining it as the *first* error reported by your compiler). As mentioned, keep the code in a function, so that others can copy the code from your question, compile it, and reproduce the error, if they so desire. – JaMiT Jul 23 '23 at 09:38
  • 1
    Youtube is NOT the best place to learn C++ in general. Better sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Jul 23 '23 at 09:40
  • Also in current C++ don't use functions like `strchr` which are still kind of there for legacy "C" compatibility, Also consider replacing `char*` (raw pointers should be considered non-owning) when you need constant strings. Use `constexpr std::string_view` and then you can use [`std::string_view::find'](https://en.cppreference.com/w/cpp/string/basic_string_view/find). – Pepijn Kramer Jul 23 '23 at 09:43
  • I am so happy for your help. I understand that I should learn C++ because I am new. Please tell me your recommendations for learning C++. – engineerMete Jul 23 '23 at 10:04
  • https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – jabaa Jul 23 '23 at 10:58
  • Thank you so much, I fixed the problem.. – engineerMete Jul 23 '23 at 15:39
  • Getting better. There is now a full error message to work off of. However, the error message does not match your code. The first error occurs on line 2593 of `beginner.cpp`. (Fortunately, there are not thousands of lines in the question's code.) This should be an easy thing to fix. Just go to one of the online C++ compilers (there is a list about 2/3 down the [C++ tag info page](https://stackoverflow.com/tags/c%2b%2b/info)), paste in the code from the question, then copy the updated error message into the question, replacing the old. – JaMiT Jul 24 '23 at 02:56

1 Answers1

1

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

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226