Char array and classes

This is a discussion on Char array and classes within the C++ Programming forums, part of the General Programming Boards category; I am writing a program for one of my classes right now and I am having some problems. I am ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    4

    Char array and classes

    I am writing a program for one of my classes right now and I am having some problems. I am a beginner programmer and just learned classes. Here is my code (took out some stuff that is not needed to solve the problem):

    Code:
    class Book{
        private:
            char BookName[40];
            int BookId;
            unsigned int numCopies;
            unsigned int copiesAvail;
        public:
            Book(char *, int , int); 
    };
    
    Book::Book(char *b, int i, int c):BookName(b),BookId(i),numCopies(c),copiesAvail(c){}
    
    int main(){
        Book a;
    }
    Here is the error I get in the Terminal:

    lib.cpp: In function `int main()':
    lib.cpp:103: error: no matching function for call to `Book::Book()'
    lib.cpp:21: error: candidates are: Book::Book(const Book&)
    lib.cpp:44: error: Book::Book(char*, int, int)

    the constructor exists, but it tells me that there is no such function. Can anyone pinpoint my error? what am I doing wrong?

    thanks for any input

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    There is no constructor that takes 0 arguments. The only constructor you have for Book is one that takes a char* and two ints. You can either implement a constructor that takes 0 arguments or do something like this:
    Code:
    int main()
    {
        Book a(NULL,0,0);
    }
    [edit]By the way, I don't think you can initialize an array like that. You'll need to do a strcpy to get the name from the char* argument into your array.
    Last edited by pianorain; 04-25-2005 at 12:18 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Code:
    Book a;
    
    ...
    
    lib.cpp: In function `int main()':
    lib.cpp:103: error: no matching function for call to `Book::Book()'
    lib.cpp:21: error: candidates are: Book::Book(const Book&)
    lib.cpp:44: error: Book::Book(char*, int, int)
    You haven't supplied a default constructor for your class which is required in order to get the above bit of code to compile. If you don't want a default constructor, you must supply the necessary arguments when you construct an object of this type.

    Code:
    Book a("Grapes of Wrath",20,20);
    Also...

    Code:
    Book::Book(char *b, int i, int c):BookName(b),BookId(i),numCopies(c),copiesAvail( c){}
    This is wrong, you can't assign a character pointer to an array. You need to use strcpy.

    Code:
    Book::Book(char *b, int i, int c):BookId(i),numCopies(c),copiesAvail(  c)
    {
        strcpy(BookName,b);
    }
    Or better yet, use a string container instead of a character array and then you can use your original syntax.

    [edit]Beaten... drat![/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    thanks for the quick reply, got it working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of classes?
    By dream_noir in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 11:43 AM
  2. Char array
    By silentkarma in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2008, 04:05 PM
  3. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 11:57 AM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM

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