14 Replies - 3635 Views - Last Post: 22 March 2011 - 08:00 AM Rate Topic: -----

#1 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 11:57 AM

I am a C++ beginner and I am trying to solve this:

Create an array of Books objects and display the books published in a year equal with a year read from standard input.


But no matter what I do I can not build my array of books. I keep stumbling in this errors and I do not know what they mean.
Could you please tell me what is wrong with my code?




//Book.h
#include <iostream>
using namespace std;

class Book
{
private:
    string title;
    string author;
    int publishing_year;

public:
    Book();
    Book(string title, string author, int publishing_year);
    ~Book();


    void Dispay_function();
    void init(string title, string author, int publishing_year);
};

//book.cpp
#include<iostream>
#include<string>
using namespace std;

#include "Book.h"

//Constructotr:
Book::Book(string title, string author, int publishing_year
{
title=t;
author=a;
publishing_year=pb;
    cout<<"**Constructor**"<<endl;
    cout<<"Book title: "<<title<<endl;
    cout<<"Author: "<<author<<endl;
    cout<<"Publishing year "<<publishing_year<<endl;
};

//Destructor:
Book::~Book()
{
cout<<"****Destructor****"<<endl;
    cout<<endl;
};

//Display Function:
void Book::Dispay_function()
{int year;
    cout<<"Input year to Search:";
    cin>>year;
    if (publishing_year==year)
    {
        cout<<"Found the book:"<<endl;
        cout<<endl;
        cout<<"Book title"<<title<<endl;
        cout<<"Author "<<author<<endl;
        cout<<"Publishing year "<<publishing_year<<endl;
        }
    else
        cout<<"No book published in "<<year<<endl;
};

void Book::init(string title, string author, int publishing_year)
{
    Book.title=title;
    Book.author=author;
    book.publishing_year=publishing_year;
};

//main.cpp
#include <iostream>
#include <string>
using namespace std;

#include "Book.h"

int main()
  {
      Book b[10] =new Book[10];
        b[1]=new Book( "The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950);
        b[2]=new Book ("Lolita", "Vladimir Nabokov", 1955);
        b[3]=new Book( "Anne of Green Gables", "Lucy Maud Montgomery", 1908);
        b[3] =new Book ("O Alquimista (The Alchemist)", "Paulo Coelho", 1988);
        b[4] =new Book("Harry Potter and the Deathly Hallows", "J.K. Rowling", 2007);
        b[5] = new Book("Angels and Demons", "Dan Brown",2000);
        b[6]=new Book ("Wolf Totem", "Jiang Rong",2004);

    for(int i=0;i<=7;i++)
        b[i].print();

    return 0;

}




Is This A Good Question/Topic? 0
  • +

Replies To: Error: "no match for operator" when building an array of Books

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 12:02 PM

tell us what errors you are getting at which points in your code :)

But first of all I'm going to complain that you have semi-colons after your function definitions
void function
{
    //...
}; // there is not supposed to be a semi-colon here

void function
{
    //...
} // this is correct



and in your header you are including iostream but not including string, yet you use the string library
Was This Post Helpful? 1
  • +
  • -

#3 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Re: Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 12:20 PM

I've erased those sorry.

The first error is in line 10:
Book[] b =new Book[10];
and it says: no match for 'operator='in 'b=(std::basic_string<char,char traits<...

Actually I think that I am not building that array of books properly....we were shown several methods in class , but they only confused me, and I have tried some of them, which resulted in a very messy code.....

This post has been edited by Miday8: 21 March 2011 - 12:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 12:30 PM

Book b[10] = new Book[10];

you declared a static array and set it equal to a dynamically allocated array?
b[1]=new Book( "The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950);

then you attempt to set a non-pointer to a dynamically allocated object?
when using dynamic memory allocation you need a pointer, it creates a array that you can treat as regular old array.
Book* b = new Book[10];

then yo can access these accordingly.
b[1] = Book( "The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950)

however to use the '=' operator you need to overload it.
Book& operator=(Book B)/> {
   title = B.title;
   author = B.author;
   publishing_year = B.publishing_year;
}

if you haven't learned operator overloading yet then just make a member function that takes it's place.
void Set(Book B)/> {
   title = B.title;
   author = B.author;
   publishing_year = B.publishing_year;
}

This post has been edited by ishkabible: 21 March 2011 - 12:37 PM

Was This Post Helpful? 2
  • +
  • -

#5 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Re: Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 12:30 PM

Thank you... I'll be fixing those

This post has been edited by Miday8: 21 March 2011 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1352
  • View blog
  • Posts: 4,654
  • Joined: 19-February 09

Re: Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 12:32 PM

Yes, it looks as if you are trying to create an array of Book pointers, and then create space for a Book object in memory as required.

So an array of pointers :-
      Book* b[10];



Just a note: arrays start at 0, you start at 1.

        b[1]=new Book( "The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950);


Was This Post Helpful? 1
  • +
  • -

#7 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Re: Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 12:42 PM

View Post#define, on 21 March 2011 - 12:32 PM, said:

Yes, it looks as if you are trying to create an array of Book pointers, and then create space for a Book object in memory as required.


So I can use this to allocate space?


void Set(Book B)/> {
   title = B.title;
   author = B.author;
   publishing_year = B.publishing_year;
}


if so....How exactly do I use this function in main().....I tried calling it like a regular function but that didn't go so well:
 Set(Book B)/>
      Book[]* b =new Book[10];
        b[0]=new Book( "The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950);
        b[1]=new Book ("Lolita", "Vladimir Nabokov", 1955);
        b[2]=new Book( "Anne of Green Gables", "Lucy Maud Montgomery", 1908);
        b[3] =new Book ("O Alquimista (The Alchemist)", "Paulo Coelho", 1988);
        b[4] =new Book("Harry Potter and the Deathly Hallows", "J.K. Rowling", 2007);
        b[5] = new Book("Angels and Demons", "Dan Brown",2000);
        b[6]=new Book ("Wolf Totem", "Jiang Rong",2004);




I have brand new errors:
line 9 @expected primary expresion before B @

line 9 <Set was not declared in this scope>
line 10 expected unqualified-id before [ token

This post has been edited by Miday8: 21 March 2011 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1352
  • View blog
  • Posts: 4,654
  • Joined: 19-February 09

Re: Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 12:48 PM

View PostMiday8, on 21 March 2011 - 09:42 PM, said:

So I can use this to allocate space?


void Set(Book B)/> {
   title = B.title;
   author = B.author;
   publishing_year = B.publishing_year;
}


if so....How exactly do I use this function in main().....


No, that function does not allocate memory.

You are already allocating memory space correctly in main(), by using the new keyword.

This is correct :-

b[1]=new Book( "The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950);


Was This Post Helpful? 1
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 12:56 PM

me and #define interpreted your code differently. i thought you wanted to make a dynamically allocated array of Books, #define thought you wanted to make a array of dynamically allocated books.

#define's way (array of dynamically allocated books)
Book* b[10];
b[0]=new Book( "The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950);


my way (dynamically allocated array of books)
Book* b = new Books[10];
b[0].Set(Book("The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950));



you are going to need to specify what you are trying to do a bit more clearly for one way to be the 'right' or wrong way

This post has been edited by ishkabible: 21 March 2011 - 12:57 PM

Was This Post Helpful? 2
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1352
  • View blog
  • Posts: 4,654
  • Joined: 19-February 09

Re: Error: "no match for operator" when building an array of Books

Posted 21 March 2011 - 04:43 PM

Using ishkabible's array, you can use your init() function.

// allocate memory and create ten Book objects
Book* b = new Books[10];
// set the values in the first Book object
b[0].init("The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950);


This post has been edited by #define: 21 March 2011 - 05:09 PM

Was This Post Helpful? 1
  • +
  • -

#11 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Re: Error: "no match for operator" when building an array of Books

Posted 22 March 2011 - 05:59 AM

Thank you...looks like I have a lot to fix and I did some of it but I still have troubles, here is my new code....but it still does not
//main.cpp
#include <iostream>
#include <string>
using namespace std;
#include "Book.h"

int main()
  {

      Book* b =new Book[10];
        b[0].Set(Book( "The Lion the Witch and the Wardrobe", "C. S. Lewis", 1950));
        b[1].Set(Book ("Lolita", "Vladimir Nabokov", 1955));
        b[2].Set(Book( "Anne of Green Gables", "Lucy Maud Montgomery", 1908));
        b[3].Set (Book ("O Alquimista (The Alchemist)", "Paulo Coelho", 1988));
        b[4].Set( Book("Harry Potter and the Deathly Hallows", "J.K. Rowling", 2007));
        b[5].Set(Book("Angels and Demons", "Dan Brown",2000));
        b[6].Set(Book ("Wolf Totem", "Jiang Rong",2004));


    return 0;

}

//book.cpp
#include<iostream>
#include<string>
using namespace std;

#include "Book.h"

//Constructotr:
Book::Book(string title, string author, int publishing_year)
{
    cout<<"**Constructor**"<<endl;
    cout<<"Book title: "<<title<<endl;
    cout<<"Author: "<<author<<endl;
    cout<<"Publishing year "<<publishing_year<<endl;
};

//Destructor:
Book::~Book()
{
cout<<"****Destructor****"<<endl;
    cout<<endl;
};

//Display Function:
void Book::Dispay_function()
{int year;
    cout<<"Input year to Search:";
    cin>>year;
    if (publishing_year==year)
    {
        cout<<"Found the book:"<<endl;
        cout<<endl;
        cout<<"Book title"<<title<<endl;
        cout<<"Author "<<author<<endl;
        cout<<"Publishing year "<<publishing_year<<endl;
        }
    else
        cout<<"No book published in "<<year<<endl;
}


void Book:: Set(Book B)/> {
   title = B.title;
   author = B.author;
   publishing_year = B.publishing_year;
}

//Book.h
#include <iostream>
#include <string>
using namespace std;

class Book
{
private:
    string title;
    string author;
    int publishing_year;

public:
    Book();
    Book(string title, string author, int publishing_year);
    ~Book();

    void Set(Book B)/>;
    void Dispay_function();
    void Set(Book B)/>;


};




And the errors are:
at line 19: error void book::set(Book)' can not be overloaded
at line 17: error with'void book::Set(Book)'

What does this mean? Is there any error dictionary on the internet, because I would very mychly need one.

This post has been edited by Miday8: 22 March 2011 - 06:01 AM

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,674
  • Joined: 25-December 09

Re: Error: "no match for operator" when building an array of Books

Posted 22 March 2011 - 06:28 AM

You have many errors in the code you posted.

Quote

at line 19: error void book::set(Book)' can not be overloaded


In your Book.h file in your class definition you have defined a member function named set() twice with the same type and numbers of parameters. If you want to overload a function then the definitions must be different. But you probably just want to delete one of these lines.

In your implementation file your constructor never initializes your member data variables. Also the use of the same name for the parameters and the member data variables will cause problems. If the names are the same then you must use the member resolution operator (this->title) to distinguish between the parameter and member variable.
Book::Book(string title, string author, int publishing_year)
{// This is printing out the parameters not the member variables.
    cout<<"**Constructor**"<<endl;
    cout<<"Book title: "<<title<<endl;
    cout<<"Author: "<<author<<endl;
    cout<<"Publishing year "<<publishing_year<<endl;
}

I recommend that you change the parameter names to something else. You should initialize your member variables in a initialization list.
Book::Book(string t, string a, int p : title(t), author(a), publishing_year(p)
{ ///.... The rest of your constructor

Also you have defined a constructor with no parameters but you have not implemented it.

Also is there any reason that you are trying to use dynamic memory to declare your Book array, instead of just statically defining the Book array?

Jim

This post has been edited by jimblumberg: 22 March 2011 - 06:31 AM

Was This Post Helpful? 1
  • +
  • -

#13 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Re: Error: "no match for operator" when building an array of Books

Posted 22 March 2011 - 07:22 AM

Quote

Also is there any reason that you are trying to use dynamic memory to declare your Book array, instead of just statically defining the Book array?

Jim

well I don't really know the difference, This is the method we were shown during the laboratory, I don't know how to use another one. My knowledge of C++ is rather limited. But I'll use your suggestions as well as I can.

This post has been edited by Miday8: 22 March 2011 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,674
  • Joined: 25-December 09

Re: Error: "no match for operator" when building an array of Books

Posted 22 March 2011 - 07:31 AM

Instead of this:
 Book* b =new Book[10];

Try this:
Book b[10];


This will create an array of 10 Books.

Jim
Was This Post Helpful? 1
  • +
  • -

#15 Miday8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-March 11

Re: Error: "no match for operator" when building an array of Books

Posted 22 March 2011 - 08:00 AM

View Postjimblumberg, on 22 March 2011 - 07:31 AM, said:

Instead of this:
 Book* b =new Book[10];

Try this:
Book b[10];


This will create an array of 10 Books.

Jim


Well than I'd better go to the library for some serious background information..I'm lacking too many fundamental notions apparently...and maybe I'll find that error dictionary.



Thank you for your time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1