Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compilation errors in a vector problem

Reply
Thread Tools

Compilation errors in a vector problem

 
 
Ankur Arora
Guest
Posts: n/a
 
      12-02-2009
While coding an algorithm for the following problem, there are a few
compilation errors that I don't completely understand. The errors
are:-

error C2839: invalid return type 'Human **' for overloaded 'operator -
>'


error C2039: 'PrintFamilyTree' : is not a member of
'std::_Vector_iterator<_Ty,_Alloc>'
1> with
1> [
1> _Ty=Human *,
1> _Alloc=std::allocator<Human *>
1> ]

I think C2839 probably triggers C2039, however I'm unable to pinpoint
the problem.

Here's the code and solution.
-----------------------------------------------------------------------------------------------------------
Implement method PrintFamilyTree() that prints out the Name and
Generation of the tree.

Output should resemble
Name: Jan Generation:0
Name: Mike Generation:1
Name: Greg Generation:2
Name: Carol: Generation: 2
Name: Peter Generation: 3
Name: Marcia Generation: 3
Name: Bobby Generation: 1


class Human : public std::vector<Human *>
{
public:
Human(const std::string &name) : m_Name(name) {};
virtual void PrintFamilyTree(const short &generation = 0) const;
protected:
std::string m_Name;
};

class Male: public Human
{
public:
Male(const std::string &name) : Human(name) {};
};

class Female: public Human
{
public:
Female(const std::string &name) : Human(name) {};
};

void main()
{
Male m1("Mike"), m2("Greg"), m3("Peter"), m4("Bobby");
Female f1("Carol"), f2("Marcia"), f3("Jan");
m1.push_back(&m2);
f1.push_back(&m3);
f1.push_back(&f2);
m1.push_back(&f1);
f3.push_back(&m1);
f3.push_back(&m4);
f3.PrintFamilyTree();
}

Solution
------------

void PrintFamilyTree(const short& generation)
{
printf("Name : %s Generation = %d\n", m_Name.c_str(),generation);
vector<Human*>::iterator it;
for (it = this.begin(); it< this.end() ;it++)
{
it->PrintFamilyTree(generation+1);
}
}

-----------------------------------------------------------------------------------------------------------
The problem occurs at the line:-
it->PrintFamilyTree(generation+1);
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-02-2009
Ankur Arora wrote:
> While coding an algorithm for the following problem, there are a few
> compilation errors that I don't completely understand. The errors
> are:-
>
> error C2839: invalid return type 'Human **' for overloaded 'operator -
>> '

>
> error C2039: 'PrintFamilyTree' : is not a member of
> 'std::_Vector_iterator<_Ty,_Alloc>'
> 1> with
> 1> [
> 1> _Ty=Human *,
> 1> _Alloc=std::allocator<Human *>
> 1> ]
> [..]
>
> void PrintFamilyTree(const short& generation)
> {
> printf("Name : %s Generation = %d\n", m_Name.c_str(),generation);
> vector<Human*>::iterator it;
> for (it = this.begin(); it< this.end() ;it++)


What's "this" doing here? As I understand the language rules, the line
above should not compile. Or is that function inside a class
definition? Then it still not supposed to compile since 'this' is a
pointer and with a pointer '.' cannot be used (pointers don't have members).

Also, it is more idiomatic to use != when comparing an iterator to the
end of the container being iterated, not <.

> {
> it->PrintFamilyTree(generation+1);
> }
> }
>
> -----------------------------------------------------------------------------------------------------------
> The problem occurs at the line:-
> it->PrintFamilyTree(generation+1);


Are you sure?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-02-2009
* Ankur Arora:
> While coding an algorithm for the following problem, there are a few
> compilation errors that I don't completely understand. The errors
> are:-
>
> error C2839: invalid return type 'Human **' for overloaded 'operator -
>> '

>
> error C2039: 'PrintFamilyTree' : is not a member of
> 'std::_Vector_iterator<_Ty,_Alloc>'
> 1> with
> 1> [
> 1> _Ty=Human *,
> 1> _Alloc=std::allocator<Human *>
> 1> ]
>
> I think C2839 probably triggers C2039, however I'm unable to pinpoint
> the problem.
>
> Here's the code and solution.
> -----------------------------------------------------------------------------------------------------------
> Implement method PrintFamilyTree() that prints out the Name and
> Generation of the tree.
>
> Output should resemble
> Name: Jan Generation:0
> Name: Mike Generation:1
> Name: Greg Generation:2
> Name: Carol: Generation: 2
> Name: Peter Generation: 3
> Name: Marcia Generation: 3
> Name: Bobby Generation: 1
>
>
> class Human : public std::vector<Human *>
> {
> public:
> Human(const std::string &name) : m_Name(name) {};
> virtual void PrintFamilyTree(const short &generation = 0) const;
> protected:
> std::string m_Name;
> };
>
> class Male: public Human
> {
> public:
> Male(const std::string &name) : Human(name) {};
> };
>
> class Female: public Human
> {
> public:
> Female(const std::string &name) : Human(name) {};
> };
>
> void main()


Your instructor or book should not teach you such bad habits.

'void' is not permitted as result type of 'main'.

Not in C, not in C++.

Use 'int main'.


> {
> Male m1("Mike"), m2("Greg"), m3("Peter"), m4("Bobby");
> Female f1("Carol"), f2("Marcia"), f3("Jan");
> m1.push_back(&m2);
> f1.push_back(&m3);
> f1.push_back(&f2);
> m1.push_back(&f1);
> f3.push_back(&m1);
> f3.push_back(&m4);
> f3.PrintFamilyTree();
> }
>
> Solution
> ------------
>
> void PrintFamilyTree(const short& generation)


This does not match the declaration in the class. Worse, you're not definining
the class' member routine but some free-standing routine.

To get going on this, replace above with

void Human:rintFamilyTree(const short& generation) const

The logic below is sound, i.e. it'll work, but the coding is ungood.

You'll get a host of compilation errors. Just fix them one by one.


> {
> printf("Name : %s Generation = %d\n", m_Name.c_str(),generation);
> vector<Human*>::iterator it;
> for (it = this.begin(); it< this.end() ;it++)
> {
> it->PrintFamilyTree(generation+1);
> }
> }



Cheers & hth.,

- Alf
 
Reply With Quote
 
Ankur Arora
Guest
Posts: n/a
 
      12-02-2009
On Dec 2, 10:14*am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Ankur Arora wrote:
> > While coding an algorithm for the following problem, there are a few
> > compilation errors that I don't completely understand. The errors
> > are:-

>
> > error C2839: invalid return type 'Human **' for overloaded 'operator -
> >> '

>
> > error C2039: 'PrintFamilyTree' : is not a member of
> > 'std::_Vector_iterator<_Ty,_Alloc>'
> > 1> * * * *with
> > 1> * * * *[
> > 1> * * * * * *_Ty=Human *,
> > 1> * * * * * *_Alloc=std::allocator<Human *>
> > 1> * * * *]
> > [..]

>
> > void PrintFamilyTree(const short& generation)
> > {
> > * printf("Name : %s Generation = %d\n", m_Name.c_str(),generation);
> > * vector<Human*>::iterator it;
> > * for (it = this.begin(); it< this.end() ;it++)

>
> What's "this" doing here? *As I understand the language rules, the line
> above should not compile. *Or is that function inside a class
> definition? *Then it still not supposed to compile since 'this' is a
> pointer and with a pointer '.' cannot be used (pointers don't have members).
>
> Also, it is more idiomatic to use != when comparing an iterator to the
> end of the container being iterated, not <.
>
> > * {
> > * * *it->PrintFamilyTree(generation+1);
> > * }
> > }

>
> > -----------------------------------------------------------------------------------------------------------
> > The problem occurs at the line:-
> > it->PrintFamilyTree(generation+1);

>
> Are you sure?
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Oh. Sorry for pasting the wrong code.
Yes 'PrintFamilyTree' is a member function and I'm using 'this->' as
against 'this.'.
Yes those are the line which the compiler is reporting errors on.
But I suspect that the line

for (it = this->begin(); it< this->end() ;it++)

is probably where the errors are originating from.
From looking at the class hierarchy (i.e. how its derived from
std::vector<Human*>, do you think this is the right way to iterate
over the elements?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-02-2009
Ankur Arora wrote:
> Oh. Sorry for pasting the wrong code.
> Yes 'PrintFamilyTree' is a member function and I'm using 'this->' as
> against 'this.'.
> Yes those are the line which the compiler is reporting errors on.
> But I suspect that the line
>
> for (it = this->begin(); it< this->end() ;it++)
>
> is probably where the errors are originating from.
> From looking at the class hierarchy (i.e. how its derived from
> std::vector<Human*>, do you think this is the right way to iterate
> over the elements?


It's the right way. The only problem is that when you dereference 'it',
the resulting value is still a pointer (since 'this' is a vector of
pointers to Human). So you need two indirections here:

(*it)->PrintFamilyTree(generation+1);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57