我试图通过引用将一个“Piece”(我定义的类)的向量传递给一个函数,但是在编译期间我得到一个错误,说“Piece”是未定义的。目前的文件如下:
Header.hpp
#pragma once
#ifndef MY_CLASS_H // Will only be true the once!
#define MY_CLASS_H
#include <vector>
//some stuff
bool make_move(std::vector<Piece*> & vector_of_pieces);
class Piece {
//member functions declarations
};
#endif
nonPieceFunctions.cpp
#include "Header.hpp"
#include<iostream>
#include <array>
#include <stdio.h>
#include <ctype.h>
using namespace std;
bool make_move(vector<Piece*> &vector_of_pieces) {
string piece_location, piece_new_location;
cout << "Which piece would you like to move?" << endl;
while (is_valid_input(piece_location) == false) { //checks input is of the right format
cin >> piece_location;
}
int file_no = get_file_no(piece_location); //gets file number from user input
int rank_no = get_rank_no(piece_location); //gets rank number from user input
cout << "File no is " << file_no << endl;
cout << "Rank no is " << rank_no << endl;
return true;
}
这不是我想要制作的函数的完整版本,但它目前不能按原样编译。 “make_move”中的任何函数都不是任何类的成员函数。
答案 0 :(得分:3)
目前正是
bool make_move(std::vector<Piece*> & vector_of_pieces);
出现在Piece
的定义之前,因此是您的问题。
在类定义之后移动它。
或转发声明如下:
class Piece;
bool make_move(std::vector<Piece*> & vector_of_pieces);
因为你在向量中使用指针它应该可以工作。
BTW - 你在某处删除这些指针吗?
答案 1 :(得分:1)
在make_move()减速之前实现类Piece:
#pragma once
#ifndef MY_CLASS_H // Will only be true the once!
#define MY_CLASS_H
#include <vector>
//some stuff
class Piece {
//member functions declarations
};
bool make_move(std::vector<Piece*> & vector_of_pieces);
#endif
或在课堂上使用前进减速:
#pragma once
#ifndef MY_CLASS_H // Will only be true the once!
#define MY_CLASS_H
#include <vector>
//some stuff
class Piece;
bool make_move(std::vector<Piece*> & vector_of_pieces);
class Piece {
//member functions declarations
};
#endif
答案 2 :(得分:0)
您在Piece
&#39;中使用make_move
s声明,你需要告诉编译器它将在以后跟进:
class Piece;
bool make_move(std::vector<Piece*> & vector_of_pieces);
class Piece {
//member functions declarations
};