【发布时间】:2013-12-26 16:15:45
【问题描述】:
我和C++同时开始学习SFML,不明白RenderWindow案例中&和*的使用规则。你能帮帮我吗?
主类:
#include <SFML/Graphics.hpp>
#include "Square.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
Square sq(5,5);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
sq.draw(&window);
window.display();
}
return 0;
}
方头:
#ifndef SQUARE_H
#define SQUARE_H
class Square
{
private:
sf::RenderWindow* window;
sf::RectangleShape rectangle;
int y;
int x;
public:
Square(int coordX, int coordY);
Square();
void draw(const sf::RenderWindow* target);
};
#endif
方形类:
#include <SFML/Graphics.hpp>
class Square{
sf::RectangleShape rectangle;
int y;
int x;
public:
Square(int coordX, int coordY)
: rectangle(), y(coordY),x(coordX)
{
rectangle.setSize(sf::Vector2f(10,100));
rectangle.setOrigin(5,50);
}
Square()
: rectangle(), y(5),x(5)
{
rectangle.setSize(sf::Vector2f(10,100));
rectangle.setOrigin(5,50);
}
void draw(sf::RenderWindow* target)
{
target->draw(rectangle);
}
我无法在 RenderWindow 上绘制正方形:
main.cpp:(.text+0x17d): undefined reference to `Square::Square(int, int)'
main.cpp:(.text+0x211): undefined reference to `Square::draw(sf::RenderWindow const*)'
我怎样才能做到这一点?
【问题讨论】:
-
你是否在 square.cpp 中包含了 square.h?
-
是的,现在的错误是:square.cpp:4:7re-initializing «class Square» square.h:3:7:previous initializing «class Square» 我把';'在标头中的 #endif 之前并在 .cpp 中删除
-
那是因为你在 square.cpp 中重新声明了这个类。您只需要提供函数实现,而不是重新定义所有内容。
-
是的!我做到了!谢谢。