Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Window.draw no instance of overloaded function  (Read 3043 times)

0 Members and 1 Guest are viewing this topic.

Jadw1

  • Newbie
  • *
  • Posts: 2
    • View Profile
Window.draw no instance of overloaded function
« on: October 16, 2018, 10:53:58 pm »
Hi, I have a problem with drawing my drawable object.

So:
I have a class that implements sf::Drawable
Node.h
#pragma once
#include <SFML/Graphics.hpp>
class Node : public sf::Drawable {
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates status) const;
}
 

Node.cpp
#include "Node.h"

void Node::draw(sf::RenderTarget& target, sf::RenderStates status) const {
sf::RectangleShape rect(...);
target.draw(rect);
}
 

In my app class, where I have pointer to window i have
Node node();

/* in loop */
...
_window->draw(node);
...
 

and there I have that error:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Window.draw no instance of overloaded function
« Reply #1 on: October 17, 2018, 12:08:27 am »
Variable 'node' type is 'Node()' not 'Node'.

Remove '()' after 'node' from the 'Node node();' line.

This is a common C++ trap: https://en.wikipedia.org/wiki/Most_vexing_parse
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Window.draw no instance of overloaded function
« Reply #2 on: October 17, 2018, 10:03:10 am »
Note that the "most vexing parse" is more specific, as shown in the wikipedia article. Here he just declared a function thinking it was a default-constructed instance, but it's more a developer oversight than a language trick ;)
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Window.draw no instance of overloaded function
« Reply #3 on: October 17, 2018, 11:00:53 am »
It's not very specific, it's not a trick, it's an ambiguity in the syntax, it's whenever something surprising arises from the C++ rule that says anything looking like a function is parsed as a function. Herb Sutter even lists trying to call default constructor as one in b): https://herbsutter.com/2013/05/09/gotw-1-solution/

Clang's -Wvexing-parse warns against this too:
[ff@localhost a]$ cat a.cpp
class Wat {};
int main()
{
    Wat wat();
}
[ff@localhost a]$ clang++ -Wvexing-parse a.cpp
a.cpp:4:12: warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
    Wat wat();
           ^~
a.cpp:4:12: note: remove parentheses to declare a variable
    Wat wat();
           ^~
1 warning generated.
« Last Edit: October 17, 2018, 11:15:27 am by FRex »
Back to C++ gamedev with SFML in May 2023

 

anything