72560

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

Question:

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?

Answer1:

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);

Answer2:

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

Answer3:

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 <a href="http://www.cplusplus.com/reference/string/string/" rel="nofollow">c_str</a>. 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.

Recommend

  • Rails loads all stylesheets except application.css.scss
  • Turning a Mongoose seeding script into a promise
  • CLion prints the input back
  • How to allow user to do input more than twice?
  • How do I add data at the end of a file in C++?
  • orderby not found error in LINQ to Entities
  • Exporting SAS DataSet on to UNIX as a text file…with delimiter '~|~'
  • Fraction length
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • c++ regex_replace not doing intended substitution
  • Using Sax parsing to edit and write XML in VB6
  • IE7 and TinyMCE with Plone
  • Reduction and collapse clauses in OMP have some confusing points
  • Moving Android View and preventing onDraw to be called over and over again
  • Is playing sound in Javascript performance heavy?
  • HTTP/2 streams vs HTTP/1.1 connections
  • C++ Partial template specialization - design simplification
  • Django: Count of Group Elements
  • Is there a javascript serializer for JSON.Net?
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • Where to put my custom functions in Wordpress?
  • Do I've to free mysql result after storing it?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • unknown Exception android
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal