首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用另一个结构函数的C++结构函数体,相同模块

调用另一个结构函数的C++结构函数体,相同模块
EN

Stack Overflow用户
提问于 2014-12-16 06:14:19
回答 4查看 2.4K关注 0票数 0

我正在尝试使用ChessPiece结构和ChessGame结构来制作象棋模块。使用XCode 6.1.1。下面是我的Chess.cpp中存在问题的头文件和函数。我得到了一个错误,“使用未声明的标识符'initChessPiece‘,您的意思是’ChessPiece::initChessPiece‘吗?”。如果我更改了它,它表示错误,’调用不带对象参数的非堆栈成员函数‘。最后,如果我做行,

代码语言:javascript
复制
game.pieces[i].initChessPiece(game.pieces[i], color, piece, x, y);

链接器抛出错误:

建筑x86_64的未定义符号:"ChessPiece::initChessPiece(ChessPiece,std::__1::basic_string,std::__1::allocator > const&,std::__1::basic_string,std::__1::allocator > const&,unsigned int,unsigned int,unsigned int)",引用于:readChessGame(棋盘&,std::__1::basic_string,std::__1::allocator > const&)在Chess.o :符号(S)中没有找到用于体系结构的x86_64 clang: x86_64: linker命令失败,退出代码1(使用-v查看调用)

代码语言:javascript
复制
#ifndef CHESS_H
#define CHESS_H

#include <stdio.h>
#include <string>

using namespace std;

const int ROWS = 8;
const int COLUMNS = 8;

struct ChessPiece {

string name;
string colour;
unsigned int row;
unsigned int column;
void initChessPiece(ChessPiece, const string& colour, const string& name, unsigned int row, unsigned int column);
string getStringColourChessPiece(const ChessPiece&) const;
string getStringNameChessPiece(const ChessPiece&) const;

friend class ChessGame;
};

struct ChessGame {
unsigned int chessBoard[ROWS][COLUMNS];
ChessPiece pieces[32];

void readChessGame(ChessGame&, const string& filename);
void printChessGame(const ChessGame&);
int scoreChessGame(const ChessGame&) const;
bool isFinished(const ChessGame&) const;
};


#endif

Chess.cpp

代码语言:javascript
复制
#include "Chess.h"
void readChessGame(ChessGame& game, const string& filename) {

ifstream inData;
inData.open(filename.c_str());

string color;
string piece;
unsigned int x;
unsigned int y;
for (int i=0;i<32;i++) {
    inData >> color >> piece >> x >> y;
    initChessPiece(game.pieces[i], color, piece, x, y);
}
}

void initChessPiece(ChessPiece& piece, const string& colour, const string& name, unsigned int row, unsigned int column) {
piece.row = row;
piece.column = column;
piece.name = name;
piece.colour = colour;
}

这是我的CS最后练习问题,所有函数头都是由指令设置的,所以我需要处理它们的设置方式。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-12-16 06:29:11

您需要一个ChessPiece类型的对象才能调用initChessPiece()

就像这样:

代码语言:javascript
复制
#include "Chess.h"
void readChessGame(ChessGame& game, const string& filename) {

    ifstream inData;
    inData.open(filename.c_str());

    string color;
    string piece;
    unsigned int x;
    unsigned int y;
    for (int i=0;i<32;i++) {
        inData >> color >> piece >> x >> y;
        game.pieces[i].initChessPiece(game.pieces[i], color, piece, x, y);
    }
}

您可能应该删除部件参数,因为您将通过this指针使函数中的对象可用。如果您决定保留它,则需要使其成为引用或指针,以便能够更改数组中各部分的值。

代码语言:javascript
复制
 game.pieces[i].initChessPiece(color, piece, x, y);

EDIT2: initChessPiece::initChessPiece( const string& colour,const string& name,unsigned int row,unsigned int列){ this->name = name;this->colour = colour;this->row = row;this->column =column};

我认为您也应该考虑将其作为构造函数。

编辑:如果你想保持你的调用风格,你可以使initChessPiece()是静态的。那么它应该可以在没有对象的情况下调用。

票数 1
EN

Stack Overflow用户

发布于 2014-12-16 06:22:49

问题在于您对方法调用语法的误解。如果类的方法是非静态的(这意味着类的每个对象都有这个方法,并且当这个方法访问类的字段时,它会访问该精确对象的字段),那么您应该告诉编译器要调用这个方法的对象是哪个。所以你应该重写你的代码:

代码语言:javascript
复制
void initChessPiece(ChessPiece, const string& colour, const string& name, unsigned int row, unsigned int column);

代码语言:javascript
复制
void initChessPiece(const string& colour, const string& name, unsigned int row, unsigned int column);

并以如下方式调用该方法:

代码语言:javascript
复制
game.pieces[i].initChessPiece(color, piece, x, y);

然后取具体对象game,然后取其具体字段game.pieces[i],然后从该具体字段调用方法initChessPiece

票数 0
EN

Stack Overflow用户

发布于 2014-12-16 06:26:39

尝试将initChessPiece的签名更改为:

代码语言:javascript
复制
void initChessPiece(const string& colour, const string& name, unsigned int row, unsigned int column);

然后使用initChessPiece ()语法调用object.Member(),如下所示:

代码语言:javascript
复制
#include "Chess.h"
void readChessGame(ChessGame& game, const string& filename) {

ifstream inData;
inData.open(filename.c_str());

string color;
string piece;
unsigned int x;
unsigned int y;
for (int i=0;i<32;i++) {
    inData >> color >> piece >> x >> y;
    game.pieces[i].initChessPiece(color, piece, x, y);
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27498631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档