我正在使用C ++(使用SFML2.2库,Xcode 6.1.1)编写棋盘游戏。我创建了两个类:块和板。我们的想法是在板内创建一个4x4的块矢量。代码如下:
在block.h中
#ifndef __Game1024__block__
#define __Game1024__block__
#include <stdio.h>
#include <iostream>
#endif /* defined(__Game1024__block__) */
using namespace std;
class block{
public:
sf::RectangleShape rect;
sf::Text text;
block();
block(const int X,const int Y,const double size, const double blockSize, const double charSize, const int value, const sf::Color fillColor, const sf::Color textColor);
};
在block.cpp
中#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include "block.h"
using namespace std;
block::block(){}
block::block(const int X,const int Y,const double size, const double blockSize, const double charSize, const int value, const sf::Color fillColor, const sf::Color textColor){
double centerX = (X+0.5)*size;
double centerY = (Y+0.5)*size;
stringstream ss;
ss << value;
string sval = ss.str();
rect.setSize(sf::Vector2f(blockSize,blockSize));
rect.setOrigin(blockSize/2.0, blockSize/2.0);
rect.setPosition(centerX, centerY);
rect.setFillColor(fillColor);
text.setStyle(sf::Text::Bold);
text.setCharacterSize(charSize);
text.setString(sval);
text.setColor(textColor);
text.setOrigin(text.getLocalBounds().width/2.0, text.getLocalBounds().height);
text.setPosition(centerX, centerY);
}
在board.h中
#ifndef __Game1024__board__
#define __Game1024__board__
#include <stdio.h>
#endif /* defined(__Game1024__board__) */
#include <vector>
#include "block.h"
using namespace std;
class board{
private:
int winSizeX;
int winSizeY;
int margin;
double charSize;
public:
vector<vector<block>> matrix;
board();
board(int winSizeX_,int winSizeY_,int margin_, double charSize_);
void draw();
};
并在board.cpp
#include "board.h"
//#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "block.h"
using namespace std;
board::board(){}
board::board(int winSizeX_,int winSizeY_,int margin_, double charSize_){
winSizeX = winSizeX_;
winSizeY = winSizeY_;
margin = margin_;
charSize = charSize_;
matrix = vector<vector<block>>(4,vector<block>(4));
double size = winSizeX/4.0; // = 256
double blockSize = size - 2*margin;
const sf::Color fillColor = sf::Color::Red;
const sf::Color textColor = sf::Color::White;
sf::Font font;
if(!font.loadFromFile("./sansation.ttf")){
exit(EXIT_FAILURE);
};
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 4; ++j){
int value = 4*j + i + 1;
matrix[i][j] = block(i,j,size,blockSize, charSize,value,fillColor,textColor);
matrix[i][j].text.setFont(font);
matrix[i][j].text.setOrigin(matrix[i][j].text.getLocalBounds().width/2.0,b[i][j].text.getLocalBounds().height);
}
}
}
错误来自 board.cpp
matrix[i][j] = block(i,j,size,blockSize, charSize,value,fillColor,textColor);
一个奇怪的事情是当我键入matrix [i] [j] = block时,我的IDE(Xcode)的自动填充将生成
matrix[i][j] = block(<#const int X#>, <#const int Y#>, <#const double size#>, <#const double blockSize#>, <#const double charSize#>, <#const int value#>, <#const int fillColor#>, <#const int textColor#>)
构造函数块的最后两个参数从 sf :: Color 类型更改为 int 。
如果我在 main.cpp 文件中创建4x4块矢量并调用块的构造函数,则没有错误...
有任何帮助吗? 提前致谢!
[更新]我附上了错误信息的scrrenshot
答案 0 :(得分:0)
谢谢@molbdnilo
我犯了一个愚蠢的错误,我忘了将SFML标题包含在&#34; block.h&#34;
#include <SFML/Graphics.hpp>
现在问题解决了!谢谢你的所有建议。我是C ++项目的新手,请随时在代码中说出我的所有不良做法。非常有帮助!