triple07
triple07

Reputation: 63

Error: Cannot convert parameter 1 from std::string to char*. Confused on why it is giving me this error.

So once again I need some help with this program for class. Maybe its that I'm tired but I can't seem to find the logic error I'm sure I've made here. Here is my code:

#include "Book.h"


using namespace std;

void add (char*, char*, int);
void remove (int&);
void list ();

int Count;

Book Bookshelf [4];

int main ()
{   
    string In;
    string N;
    string A;
    int Y;
    int Num;

    do
    {
        cout << "Bookshelf> ";
        getline(cin, In);

        if (In.compare("add") == 0)
        {
            cout << "Bookshelf> Enter book: ";
            cin >> N >> A >> Y;
            add (N,A,Y);
        }

        else if (In.compare ("remove") == 0)
        {
            cout << "Bookshelf> Select number: ";
            cin >> Num;
            remove (Num);
        }

        else if (In.compare("list") == 0)
        {
            list ();
        }

    } while (cin != "quit");

    return 0;
}

void add (string N, string A, int Y)
{
    if (Bookshelf[4].IsEmpty() == false)
        cout << "Error!" << endl;
    else
    {
        Bookshelf[Count] = Book (N,A,Y);
        Count++;
    }
    cout << "Bookshelf> ";
}

The error occurs at the line add(N,A,Y); but for the life of me I can't tell why it is saying that. They both look like std::strings to me. Can anyone explain this to me?

Upvotes: 0

Views: 2316

Answers (3)

Ed Heal
Ed Heal

Reputation: 59997

You need to make the declaration match the definition. In the declaration you use char * in the definistion you use string.

If you want to use C strings see c_str. If you want to use strings, remember the '&' to take a reference - saves making a copy. Either way make the prototype and the function signature match.

Upvotes: 2

Mahesh
Mahesh

Reputation: 34625

You have a wrong forward declaration.

void add (char*, char*, int);

Must be -

void add (string, string, int);

Also, if the array size is N, the accessible indexes are 0 to N-1.

Book Bookshelf [4];

// .....

if (Bookshelf[4].IsEmpty() == false)  // There is no object at Bookshelf[4]
                                      // Accessible indexes are 0 to 3

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

You have forgotten to modify your prototype at the top of your file.

It still says

void add (char*, char*, int);

It should be

void add (string, string, int);

Upvotes: 3

Related Questions