Small errors

This is a discussion on Small errors within the C++ Programming forums, part of the General Programming Boards category; I have just added two variables to an object, which was already working, to a class of mine.. author and ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    17

    Small errors

    I have just added two variables to an object, which was already working, to a class of mine.. author and isbn. Here is a section of the code.

    Code:
    #include<iostream>
    #include<fstream>
    #include<stdlib>
    #include<time>
    #include<string>
    #include<ctype>
    
    #define KBYTE 1024
    
    using namespace std;
    
    class bookobject
    {
    	protected:                        //new fields here which means
    		char name[KBYTE];              //changeing the edit function
          char author[KBYTE];            //and books2file -
          char isbn[KBYTE];              //so new constructor << =
    		int number;                    //and methods to access and change fields
                                         //keep getName() setName() getNumber() setNumber()
    
    
    	private:
    	public:
       bookobject(void)
    	{
    		strcpy(name, "");
          strcpy(author, "");
          strcpy(isbn, "");
    		number = -1;
    	}
    The errors are caused by the following lines

    char author[KBYTE];
    char isbn[KBYTE];

    I get these errors for both lines

    1."Cannot convert 'std::basic_string<char,std::string_char_traits<ch ar>,std::allocator<char>>' to 'char *'
    2."Type mismatch in parameter '_dest' in call to 'strcpy(char *, const char *)'

    Hope that's the relevent bit, didn't want to post the whole lot. Thanks! (Full code attached if needed!)
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    Your headers are incorrect. They should be like so.
    Code:
    #include<iostream>
    #include<fstream>
    #include<cstdlib>
    #include<ctime>
    #include<cstring>
    #include<cctype>
    #include<string>
    The error you were asking about is concerning two names clashing in the constructor. If the parameter names match member names, you must either change the parameter names, or prefix the member names with "this->". Here is the corrected constructor in question.
    Code:
    bookobject(string nam, string author, string isbn, int num)
    {
      strcpy(name, nam.c_str());
      strcpy(this->author, nam.c_str());
      strcpy(this->isbn, nam.c_str());
      number = num;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  2. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  3. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21