【问题标题】:Undefined reference, using SFML未定义的引用,使用 SFML
【发布时间】: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 中重新声明了这个类。您只需要提供函数实现,而不是重新定义所有内容。
  • 是的!我做到了!谢谢。

标签: c++ pointers sfml


【解决方案1】:

在 C++ 中,你有函数 declaration 和函数 definition

// declaration, typically in X.h
#pragma once // don't include this twice, my dear compiler
class X {
public:
   void foo();
};

实施:

// X.cpp
#include "X.h"
void X::foo() { ...code .... }

// here you wrote
// class X { void foo(){} };
// which is a _declaration_ of class X
// with an _inline definition_ of foo.

用法:

// main.cpp
#include "X.h"

将此模式应用于您的代码应确保

  • 每个符号都已定义
  • 没有符号被多次定义

【讨论】:

    【解决方案2】:

    删除 square.cpp 并将您的 square.h 文件更改为以下内容。请确保在更改文件之前进行备份。

    #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)
            : 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);
            }
    };
    
    #endif
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 2017-03-11
      • 2018-07-17
      • 2015-02-14
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      相关资源
      最近更新 更多