-4

Im getting these errors because i cant convert correctly const char* to std::string....

In file included from ./../Header Files/MainGame.h:5:0, from ./MainGame.cpp:2, from main.cpp:1: ./../Header Files/../Source Files/Hangman.cpp: In member function ‘void Hangman::checkIfItEquals(std::string)’: ./../Header Files/../Source Files/Hangman.cpp:55:41: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] std::string charStr = string(it) ^ In file included from /usr/include/c++/4.8/string:52:0, from /usr/include/c++/4.8/bits/locale_classes.h:40, from /usr/include/c++/4.8/bits/ios_base.h:41, from /usr/include/c++/4.8/ios:42, from /usr/include/c++/4.8/ostream:38, from /usr/include/c++/4.8/iostream:39, from ./../Header Files/MainGame.h:2, from ./MainGame.cpp:2, from main.cpp:1: /usr/include/c++/4.8/bits/basic_string.h:490:7: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT, const _Alloc&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]’ [-fpermissive] basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()); ^ In file included from ./../Header Files/MainGame.h:5:0, from ./MainGame.cpp:2, from main.cpp:1: ./../Header Files/../Source Files/Hangman.cpp:56:9: error: expected ‘,’ or ‘;’ before ‘if’ if(guessLet.compare(charStr) == 0) ^

And here is the code.... Im going to give it all if other code has any relation to the problem..

Hangman.cpp:

#include<vector>
#include "../Header Files/Hangman.h"

Hangman::Hangman()
{
}

void Hangman::run()
{
    this -> mainGame();
    this -> mainGameLoop();
}


void Hangman::mainGame()
{
    dictonary.init();
    randomString = dictonary.randomItem();
    cout << "RANDOM STRING : " << randomString << endl;
    stringToVec();
}

void Hangman::stringToVec()
{
    for(std::string::iterator it = randomString.begin(); it != randomString.end(); ++it) 
    {
        randStringVec.push_back(*it);
    }
}


void Hangman::mainGameLoop()
{
    try{
         cout << " \n Guess A Letter (MUST BE A LETTER OR EXIT) \n";
         std::cin >> userGuessLetter;
         if(userGuessLetter.length() > 1)
             throw "Too Big Of a letter";
    } 
    catch(const char* exc)
    {
       if(true)
       {
          exit(1);
       }
    }

    cout << "You typed " << userGuessLetter << endl;
}

void Hangman::checkIfItEquals(string guessLet)
{
    for(std::vector<char>::iterator it = randStringVec.begin(); it!=randStringVec.end(); it++)
    {
        std::string charStr = string(*it)
        if(guessLet.compare(charStr) == 0)
        {
            cout << "GOOD JOB.. THERE IS A SIMMILAR LETTER!" << endl;
            break;
        }
    }
}


Hangman::~Hangman()
{
}

Hangman.h:

#pragma once
#include<iostream>
#include<string>
#include<vector>
#include "../Source Files/Drawer.cpp"
#include "../Source Files/Dictonary.cpp"

using std::cout;
using std::endl;
using std::string;


class Hangman
{
public:

    Hangman();
    ~Hangman();
    void run();
private:
    Drawer hgmnDraw;
    Dictonary dictonary;
    std::vector<char> randStringVec;
    string randomString;
    string userGuessLetter;
    void mainGame();
    void drawHangMan(int attempts, int mode);
    void checkIfItEquals(string guessLet);
    void stringToVec();
    void mainGameLoop();
    int attemptsMadeBad = 0, attemptsMadeGood = 0;
    bool correctGs;
};

EDIT

After BenjaminLindley Comment below on the question i made the edit.. and elivated one error but 2 of the same errors still presist:

In file included from ./../Header Files/MainGame.h:5:0, from ./MainGame.cpp:2, from main.cpp:1: ./../Header Files/../Source Files/Hangman.cpp: In member function ‘void Hangman::checkIfItEquals(std::string)’: ./../Header Files/../Source Files/Hangman.cpp:55:41: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] std::string charStr = string(it); ^ In file included from /usr/include/c++/4.8/string:52:0, from /usr/include/c++/4.8/bits/locale_classes.h:40, from /usr/include/c++/4.8/bits/ios_base.h:41, from /usr/include/c++/4.8/ios:42, from /usr/include/c++/4.8/ostream:38, from /usr/include/c++/4.8/iostream:39, from ./../Header Files/MainGame.h:2, from ./MainGame.cpp:2, from main.cpp:1: /usr/include/c++/4.8/bits/basic_string.h:490:7: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT, const _Alloc&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]’ [-fpermissive] basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());

ANOTHER EDIT

Basically im not converting char const* correctly to string:

for(std::vector<char>::iterator it = randStringVec.begin(); it!=randStringVec.end(); it++)
    {
        std::string charStr = string(*it);
        if(guessLet.compare(charStr) == 0)
        {
            cout << "GOOD JOB.. THERE IS A SIMMILAR LETTER!" << endl;
            break;
        }
    }

As you can see on second line i am typecasting but that is not working according to this error:

In file included from ./../Header Files/MainGame.h:5:0,
                 from ./MainGame.cpp:2,
                 from main.cpp:1:
./../Header Files/../Source Files/Hangman.cpp: In member function ‘void Hangman::checkIfItEquals(std::string)’:
./../Header Files/../Source Files/Hangman.cpp:55:41: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
         std::string charStr = string(*it);
                                         ^
In file included from /usr/include/c++/4.8/string:52:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from ./../Header Files/MainGame.h:2,
                 from ./MainGame.cpp:2,
                 from main.cpp:1:
/usr/include/c++/4.8/bits/basic_string.h:490:7: error:   initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ [-fpermissive]
       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
John Doe
  • 19
  • 4
  • 1
    Look at the last line of your error message. – Benjamin Lindley May 17 '16 at 00:53
  • Ok @BenjaminLindley that help elevate that problem.. But the first two problem still presist.. i will edit in a second to the problems that still presist – John Doe May 17 '16 at 00:55
  • 1
    @JohnDoe It would help if you pointed out the line with the error. No one is going to wade through and count lines to figure out where the error is. – PaulMcKenzie May 17 '16 at 00:56
  • Ok im going to put it in format.. basically i am not converting... Sec @PaulMcKenzie Let me edit to make it more precice – John Doe May 17 '16 at 00:57
  • Ok I finished editing it @PaulMcKenzie – John Doe May 17 '16 at 00:59
  • Ok i edited it again @BenjaminLindley – John Doe May 17 '16 at 00:59
  • 2
    You `#include` your .cpp files? – kfsone May 17 '16 at 01:02
  • Yes there are 2 more files.. Errors are not even there so dont be concerened about those please.. @kfsone . Im coding this here: https://ide.c9.io/antihuman/fornoreason So thats where im working it at, if you want to see the cpp files go there..[Awesome Online IDE] – John Doe May 17 '16 at 01:04
  • @JohnDoe What is your goal? Is it to attempt to initialize a `std::string` with a single char? – PaulMcKenzie May 17 '16 at 01:04
  • Ok @PaulMcKenzie basically im trying to convert const char* to std::string – John Doe May 17 '16 at 01:07
  • Basically the opposite of this @PaulMcKenzie : http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char?rq=1 – John Doe May 17 '16 at 01:08
  • @JohnDoe - That is not what your code is doing. Where is the `const char*` in this line: `std::string charStr = string(*it);`, and `it` is an iterator that when dereferenced, is a single `char`? Did you inspect with the debugger what `*it` really is? What you are winding up doing (and failing) is taking a single char and trying to make a string out of it. – PaulMcKenzie May 17 '16 at 01:09
  • yes its a const char* look at the error it states that @PaulMcKenzie at line 5 or 6 – John Doe May 17 '16 at 01:10
  • 1
    You're not trying to convert a `const char *` to a string, you're trying to convert a `char` to a string. – user253751 May 17 '16 at 01:10
  • @JohnDoe Did *you* look at the error? "invalid conversion from `char` to `const char*`" – user253751 May 17 '16 at 01:11
  • Ahhh @immibis my mind is very confused!! How do i convert this char or stuff, to string? – John Doe May 17 '16 at 01:11
  • 1
    @JohnDoe You don't. You need to take a step back, and figure out what you actually want your program to do, instead of just changing things until it compiles. – user253751 May 17 '16 at 01:12
  • PaulMcKenzie anwsered question!! Im just stupid sorry @immibis – John Doe May 17 '16 at 01:14

1 Answers1

1

Actually, your goal seems to be to convert a single char to a std::string.

You cannot initialize a std::string with a single char. What will wind up happening is that the compiler will try and promote the char to a const char *, exactly as the error describes.

What you want is the std::string constructor that takes a single character and a count:

 std::string charStr = string(1, *it);

See the link here and look at constructor 2)

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45