Pure virtual function error

Dec 20, 2012 at 10:48pm
I have a base class with a pure virtual function, and a derived class that implements it. When I try to call this function from the sub class object, I get a message:
Request for member 'stdOutAttrib' in rect, which is of non class type Rectangle()


Now Rectangle is indeed a class, so I don't know why it thinks it isn't. Also not sure why it has it as Rectangle() with the parentheses. Virtual functions are new to me, so bare with me.
Dec 20, 2012 at 10:58pm
Lets see your code..
( if you are on Windows - you might be inadvertantly calling the Windows API Rectangle() function )
Dec 20, 2012 at 10:58pm
You probably wrote Rectangle rect();, which is a function declaration.
It should be Rectangle rect;
Last edited on Dec 20, 2012 at 11:25pm
Dec 21, 2012 at 4:11am
Why can't I use Rectangle rect(); ? Isn't that calling the default constructor?
Dec 21, 2012 at 4:32am
1
2
3
4
std::string foo ; // foo is a default constructed std::string

std::string bar() ; // declare bar
// bar is a function taking no parameters and returning a std::string 

Dec 21, 2012 at 2:08pm
I thought you did it first way (without parentheses) if there is no user- defined default constructor, and the second way if there is a user-defined default constructor?

I don't really see the difference here
Dec 21, 2012 at 2:22pm
> I thought you did it first way (without parentheses) if there is no user- defined default constructor,
> and the second way if there is a user-defined default constructor?

No.


> I don't really see the difference here

Try compiling this:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct A { A() { /* default constructor */ } A(int) { /* constructor with arg */ } } ;

int main()
{
    A one ; // one: default-constructed A (definition)
    A* pa = &one ; // fine.

    A two(23) ; // two: object of type A initialized with 23 (definition)
    pa = &two ; // also fine

    A three() ; // three: function returning an A and taking no args (declaration)
    pa = &three ; // *** error: can't convert from pointer to function to pointer to A
}
Dec 21, 2012 at 2:51pm
This is one of those issues where Stroustrup tells everyone to forgo the old ways and use curlies in all situations:
1
2
A one{}; // default ctor
A two{23}; // single-argument ctor 
I wonder if that will catch on.
Last edited on Dec 21, 2012 at 2:53pm
Dec 21, 2012 at 3:07pm
Dec 21, 2012 at 3:33pm
Ah now I get it! Catfish, that article was an interesting read. Though it definitely seemed to have the vibe of "Don't touch C++, it's awful" :/
Dec 21, 2012 at 4:19pm
> I wonder if that will catch on.

If uniform initialization does catch on, the overwhelming beneficiaries would be students who are starting out to learn C++; it makes C++ programming simpler and more intuitive.

Though I doubt that it will catch on. The fundamental problem with C++ is its teachers - in schools, on the web, everywhere. Fourteen years after std::vector<> and std::string, students of C++ are still not learning programming. Instead, right from day two, they are being overwhelmed by the intricacies C-style arrays and dynamic memory management.
Last edited on Dec 21, 2012 at 4:20pm
Dec 21, 2012 at 6:02pm
newGuy23423 wrote:
Though it definitely seemed to have the vibe of "Don't touch C++, it's awful" :/

That's the stated goal of that site. Don't take it seriously.
Topic archived. No new replies allowed.