为“Mastermind”游戏编写程序。在我的一个类代码中,我将它分成了.h和.cpp文件,我不知道为什么我的cpp文件在编译时不断给我这个错误:错误:'int code :: getVector()'的原型与'code'中的任何类都不匹配
据我所知,我在code.h文件中有getVector()的正确标题:
#include <vector>
#include <iostream>
#ifndef CODE_H
#define CODE_H
using namespace std;
class code //strores code vector and checks for correct and incorrect numbers
{
private:
vector<int> codeVector {0,0,0,0}; //vector storing code (guess and secret code depending on object)
public:
vector<int> getVector(); //retrievs private codeVector
void setCodeVectorToGuess(vector<int> guess); //sets private codeVector to vector passed in
void setCodeVectorToSecretCode(); //uses rand function to set random secret code vector
int checkCorrect(code guessObject); //uses for loop to check what values are correct number correct location by checking equivalence at each vector index
int checkIncorrect(code guessObject); //checks numbers between the two vectors using two for loops which each replace values in the vectors with non-equivalent out-of-range numbers
};
#endif // CODE_H
和我的.cpp文件中的正确语法:
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "code.h"
using namespace std;
//code :: codeVector = {0,0,0,0};
code :: getVector() //retrieves private codeVector
{
return codeVector;
}
code :: setCodeVectorToGuess(vector<int> guess) //sets private codeVector to vector passed in
{
codeVector = guess;
}
code :: setCodeVectorToSecretCode() //uses rand function to set random secret code vector
{
srand (time(NULL));
codeVector = {rand() % 5 + 1, rand() % 5 + 1, rand() % 5 + 1, rand() % 5 + 1};
}
code :: checkCorrect(code guessObject) //uses simple for loop to check what values are correct number correct location
{
int correctPlaceCorrectNum = 0;
vector<int> guessVector;
guessVector = guessObject.getVector();
for (int o=0; o<codeVector.size(); o++)
{
if (codeVector[o] == guessVector[o])
{
correctPlaceCorrectNum ++;
}
}
return correctPlaceCorrectNum;
}
code :: checkIncorrect(code guessObject) //checks numbers between the two vectors using two for loops which each replace values in the vectors with non-equivalent out-of-range numbers
{
int incorrectPlaceCorrectNum = 0;
int counter(0), counter2(0);
int array1[8] = {6,7,8,9}; //two arrays used for the elimination of vector values by substitution of non-equivalent values
int array2[8] = {10,11,12,13}; //
vector<int> secretCodeVector {0,0,0,0}; //copy of secret code vector
secretCodeVector = getVector();
vector<int> guessVector {0,0,0,0}; //copy of guess vector
guessVector = guessObject.getVector();
for (int u=0; u<secretCodeVector.size(); u++) //checks and replaces all correct number correct location values in secret code vector
{
if (secretCodeVector[u] == guessVector[u])
{
secretCodeVector[u] = array1[counter];
counter++;
}
}
for (int i=0; i<secretCodeVector.size(); i++) //checks and replaces all correct number incorrect location values in both vectors
{
for (int l=0; l<secretCodeVector.size(); l++) //checks each digit of secret code with all digits of guess code
{
if (secretCodeVector[i] == guessVector[l])
{
secretCodeVector[i] = array1[counter];
guessVector[l] = array2[counter2];
incorrectPlaceCorrectNum++;
counter++;
counter2++;
break;
}
}
}
return incorrectPlaceCorrectNum;
}
答案 0 :(得分:1)
问题是.h文件中的函数声明和.cpp文件中的定义不匹配:在.h文件中声明:
vector<int> code::getVector()
并在.cpp文件中:
code::getVector()
问题在于它们不匹配,但它们必须匹配。写
vector<int> code::getVector()
{
// code here
}
同样适用于所有其他定义。
在古代,没有返回类型意味着返回类型是int,这解释了您观察到的错误消息。但这已经被禁止了很长时间,应该发出警告。