class object manipulation

This is a discussion on class object manipulation within the C++ Programming forums, part of the General Programming Boards category; HI, little help requested. i am trying to select the members of a class object b1[i], by comparing a member->value ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    class object manipulation

    HI, little help requested.

    i am trying to select the members of a class object b1[i], by comparing
    a member->value with and external value, and if the comparision is
    true, then I would assign the entire b1[i] object to b2[i] object, after creating
    a new book().

    However I keep getting this error
    Code:
    main.cxx: In function `int main(int, char**)':
    main.cxx:107: error: no matching function for call to `book::setValues(book*&)'
    book.h:15: error: candidates are: void book::setValues(book**)
    book.h:17: error:                 void book::setValues(std::basic_string<char, 
       std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
       std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
       std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
       std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
       std::char_traits<char>, std::allocator<char> >, float)
    make: *** [main.o] Error 1
    Im not sure if its the way im calling my constructor for this member function, becuase i havent used the & operator, only the ** types. Any help or suggestions are greatly appreciated.


    main.cpp
    Code:
       float low,high;
       int nsize = 0;
       book *b2[VAL];
       for(int i = 0; i < size-1; i++) {
          if(b1[i]->getPrice() > low && b1[i]->getPrice() < high) {
             b2[i] = new book(title, author, publisher, month, year, price);
    
             b2[i]->setValues(b1[i]); // this line is causing ERROR.
    
             nsize++;
          }
    book.h
    Code:
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    class book {
       public:
          book(string,string,string,string,string,float);
          ~book();
          string getTitle() { return title; }
          string getAuthor() { return author; }
          string getPublisher() { return publisher; }
          string getMonth() { return month; }
          string getYear() { return year; }
          void setValues(book **b){}
          void setValues(string title, string author, string publisher,
                         string month, string year, float price);
          float getPrice() { return price; }
       private:
          string title;
          string author;
          string publisher;
          string month;
          string year;
          float price;
    };
    book.cpp

    Code:
    #include "book.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    book::book( string title, string author, string publisher,
                string month, string year, float price) {}
    
    void book::setValues(string title, string author, string publisher,
                         string month, string year, float price) {
       this->title=title; this->author=author; this->publisher=publisher;
       this->month=month; this->year=year; this->price=price;
    }
    
    void book::setValues(book **b) {
      b->title=this->title; b->author=this->author; b->publisher=this->publisher;
      b->month=this->month; b->year=this->year; b->price=this->price;
    }
    Last edited by guda; 10-09-2004 at 09:58 AM.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You should be accepting a pointer to a book in your SetValues function, not a double pointer. Should be:
    Code:
    void book::setValues(book *b)
    And in this case a copy constructor would work just as well. In fact, because all the members of your class are built-in types (like float) or have valid copy constructors themselves (like string), you don't even have to write a copy constructor, the default compiler generated one will work for you, so you can do this and remove the SetValues call:
    Code:
    b2[i] = new book(*(b1[i]));
    Note that the extra * is to dereference the pointer since a copy constructor takes the object itself, not a pointer.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    Yes, that does work... I tried that before, however it kept segfaulting.. I see now that the segfault occurs in display() and not in main();

    Here is display, in case anyone sees an error, tiing is, this display function works fine when passed display(b1,size), (however no sorting), so im assuming my counting is wrong somewhere.;


    Code:
    void display(book **b, int size); //constructor
    
    display(b2,nsize);
    void display(book **b, int size) {
       cout.precision(2);
       for(int i = 0; i < size-1; i++) {
       cout.setf(ios_base::left);
       cout << "|" << setw(25) << b[i]->getTitle().substr(0,25)
            << "|" << setw(20) << b[i]->getAuthor().substr(0,20)
            << "|" << setw(15) << b[i]->getPublisher().substr(0,15)
            << "|" << setw(3) << b[i]->getMonth() << "-" << setw(2) << b[i]->getYear()
            << "|" << " $" << setw(5) << b[i]->getPrice() << "|\n";
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  2. Replies: 8
    Last Post: 01-13-2008, 04:57 PM
  3. Class and Global Variable Manipulation
    By Matt3000 in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2006, 04:09 AM
  4. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 08:20 PM
  5. C++ Class Object Collection
    By Visual Develope in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2002, 04:48 PM

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