Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ *this pointer problem compiling

15 views
Skip to first unread message

nvangogh

unread,
Apr 9, 2013, 9:52:53 AM4/9/13
to

I am having problems compiling a simple user defined class, with a
constructor that takes an istream argument. I am trying to test this
constructor. The other three constructors work.

The problem is not with the istream. I cannot see how it can be with the
read function either as I have tested it with other options.

If I try to compile with a main program , i get this error:

'g++ cons4.cpp -o cons4
In file included from cons4.cpp:4:
Sales_data.h: In constructor �Sales_data::Sales_data(std::istream&)�:
Sales_data.h:32: error: invalid conversion from �void*� to �int�
Sales_data.h:32: error: cannot convert �Sales_data� to �void*� for
argument �2� to �ssize_t read(int, void*, size_t)�
Sales_data.h:33: error: returning a value from a constructor'

Note that Line 32 in the error is:

> read(is, *this);

I have been thinking about this for sometime, but cannot see what the
problem is.: The function takes two arguments an istream& and the *this
pointer. This function works - i have tested it. I do not understand why
it is failing to work within the constructor. Obviously, it is an error
to return a value from a constructor - though I have tried to do that to
see if it made a difference.

Do you haves any suggestions to fix it?

This is the contents of Sales_data.h
----------------------
#ifndef SALES_DATA_H
#define SALES_DATA_H

#include <string>
#include <iostream>

struct Sales_data {
Sales_data():bookNo(""),units_sold(0),revenue(0){}
Sales_data(const std::string &s):bookNo(s),units_sold(0),revenue(0){}
Sales_data(const std::string &s, unsigned n, double
p):bookNo(s),units_sold(n),revenue(p*n){}
Sales_data(std::istream &);

std::string isbn() const {return bookNo;}
Sales_data& combine(const Sales_data&);
double avg_price() const;

std::string bookNo;
unsigned units_sold;
double revenue;
};

// definition of 4th constructor - problem with read function
Sales_data::Sales_data(std::istream &is)
{
read(is, *this);
}

double Sales_data::avg_price()const
{
if(units_sold)
return revenue/units_sold;
else
return 0;
}

Sales_data& Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}

Sales_data add(const Sales_data&, const Sales_data&);
std::ostream& print(std::ostream&, const Sales_data&);
std::istream& read(std::istream&, Sales_data&);

Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}

std::istream& read(std::istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}

std::ostream& print(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " "
<< item.revenue << " " << item.avg_price();
return os;
}

#endif
-------------------------

Ike Naar

unread,
Apr 9, 2013, 12:15:13 PM4/9/13
to
On 2013-04-09, nvangogh <nvan...@invalid.net> wrote:
> I am having problems compiling a simple user defined class, with a
> constructor that takes an istream argument. I am trying to test this
> constructor. The other three constructors work.
>
> The problem is not with the istream. I cannot see how it can be with the
> read function either as I have tested it with other options.
>
> If I try to compile with a main program , i get this error:
>
> 'g++ cons4.cpp -o cons4
> In file included from cons4.cpp:4:
> Sales_data.h: In constructor ?Sales_data::Sales_data(std::istream&)?:
> Sales_data.h:32: error: invalid conversion from ?void*? to ?int?
> Sales_data.h:32: error: cannot convert ?Sales_data? to ?void*? for
> argument ?2? to ?ssize_t read(int, void*, size_t)?
> Sales_data.h:33: error: returning a value from a constructor'
>
> Note that Line 32 in the error is:
>
>> read(is, *this);
>
> I have been thinking about this for sometime, but cannot see what the
> problem is.: The function takes two arguments an istream& and the *this
> pointer. This function works - i have tested it. I do not understand why
> it is failing to work within the constructor. Obviously, it is an error
> to return a value from a constructor - though I have tried to do that to
> see if it made a difference.
>
> Do you haves any suggestions to fix it?

Move the declaration of the 'read' function
before the call to the function.

nvangogh

unread,
Apr 9, 2013, 12:49:17 PM4/9/13
to
On 09/04/13 17:15, Ike Naar wrote:

> Move the declaration of the 'read' function
> before the call to the function.

Why? I fail to see how you have arrived at this conclusion. The read
function works. The istream& and *this argument , however, are resulting
in the error that I posted.

Ike Naar

unread,
Apr 9, 2013, 1:24:51 PM4/9/13
to
On 2013-04-09, nvangogh <nvan...@invalid.net> wrote:
Your 'read' function, that is, the one you declared as

std::istream& read(std::istream&, Sales_data&);

may work, but that is not the point. The problem is that
this declaration appears after the call to 'read'
in the body of Sales_data::Sales_data(std::istream &is).
At the point of the call, *your* 'read' function isn't known.

It's likely that there is some other 'read' function that is
declared in one of the headers that you #include in your program.
That other 'read' function as a different prototype that does
not match the call. Hence the error message that you are seeing.

Francis Glassborow

unread,
Apr 9, 2013, 1:54:22 PM4/9/13
to
On 09/04/2013 14:52, nvangogh wrote:
>
> I am having problems compiling a simple user defined class, with a
> constructor that takes an istream argument. I am trying to test this
> constructor. The other three constructors work.
>
> The problem is not with the istream. I cannot see how it can be with the
> read function either as I have tested it with other options.
>
> If I try to compile with a main program , i get this error:
>
> 'g++ cons4.cpp -o cons4
> In file included from cons4.cpp:4:
> Sales_data.h: In constructor ‘Sales_data::Sales_data(std::istream&)’:
> Sales_data.h:32: error: invalid conversion from ‘void*’ to ‘int’
> Sales_data.h:32: error: cannot convert ‘Sales_data’ to ‘void*’ for
> argument ‘2’ to ‘ssize_t read(int, void*, size_t)’

There is the clue, it has found another overload of read(). At this
stage in your code I cannot see a declaration of the read you want to use.

Of course, the read (as should the print) in question should be a member
function and as such will only have a single parameter, an istream&.


Whilst definitions can be delayed, declarations must be nm
made before something is first used.

In general C++ does not allow the implementation to look ahead for a
declaration. Unlike the earlier versions of C which allowed implicit
declarations of functions C++ has never allowed that (almost impossible
to support in the context of overloading)

Francis
0 new messages