可能重复:
What is an undefined reference/unresolved external symbol error and how do I fix it?
嘿,我在游戏中遇到了这个错误。我有一个标题和.cpp文件的问题,所以它显然在那。但我一直在寻找标题和.cpp文件,我找不到问题。这是错误:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Questions::Questions(void)" (??0Questions@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'questions''(void)" (??__Equestions@@YAXXZ) C:\Users\Conor\Documents\College\DKIT - Year 2 - Repeat\DKIT - Year 2 - Semester 1 - Repeat\Games Programming\Millionaire\Millionaire\Main.obj Millionaire
以下是文件:
Questions.h
#ifndef QUESTIONS_H
#define QUESTIONS_H
#include <string>
#include <algorithm>
#include <map>
using namespace std;
class Questions
{
public:
Questions();
Questions(string question,string correctAnswer, string wrongAnswer1,string wrongAnswer2,string wrongAnswer3);
void shuffle(string *array, int n);
string getQuestion();
string getCorrectAnswer();
string* getAnswers();
bool checkAnswer(string answer);
void questionStore();
void addQuestion(int level, Questions *question);
Questions* printQuestion(int level);
private:
string question;
string correctAnswer;
string* pAnswers;
multimap<int,Questions*> map;
};
#endif
Questions.cpp
#include "Questions.h"
using namespace std;
Questions::Questions(string question,string correctAnswer, string wrongAnswer1,string wrongAnswer2,string wrongAnswer3)
{
this->question = question;
this->pAnswers = new string[4];
this->pAnswers[0]=wrongAnswer1;
this->pAnswers[1]=wrongAnswer2;
this->pAnswers[2]=wrongAnswer3;
this->pAnswers[3] =correctAnswer;
this->shuffle(this->pAnswers,4);
this->correctAnswer = correctAnswer;
}
void Questions::shuffle(string *array, int n)
{
random_shuffle(&this->pAnswers[0],&this->pAnswers[4]);
}
string Questions::getQuestion()
{
return this->question;
}
string Questions::getCorrectAnswer()
{
return this->correctAnswer;
}
string* Questions::getAnswers()
{
return this->pAnswers;
}
bool Questions::checkAnswer(string answer)
{
if(this->correctAnswer.compare(answer)==0)
{
return true;
}
return false;
}
void Questions::questionStore()
{
Questions *q1 = new Questions("Whats the oldest known city in the world?", "Sparta" , "Tripoli" , "Rome", "Demascus");
Questions *q2 = new Questions("What sport in the olympics are beards dissallowed?", "Judo", "Table Tennis" , "Volleyball", "Boxing");
Questions *q3 = new Questions("What does an entomologist study?", "People" , "Rocks" , "Plants", "Insects");
Questions *q4 = new Questions("Where would a cowboy wear his chaps?", "Hat" , "Feet" , "Arms", "Legs");
Questions *q5 = new Questions("which of these zodiac signs is represented as an animal that does not grow horns?", "Aries" , "Tauris" , "Capricorn", "Aquarius");
Questions *q6 = new Questions("Former Prime Minister Tony Blair was born in which country?", "Northern Ireland" , "Wales" , "England", "Scotland");
Questions *q7 = new Questions("Duffle coats are named after a town in which country?", "Austria" , "Holland" , "Germany", "Belgium");
Questions *q8 = new Questions("The young of which creature is known as a squab?", "Horse" , "Squid" , "Octopus", "Pigeon");
Questions *q9 = new Questions("The main character in the 2000 movie ""Gladiator"" fights what animal in the arena?", "Panther" , "Leopard" , "Lion", "Tiger");
addQuestion(1,q1);
addQuestion(1,q2);
addQuestion(1,q3);
addQuestion(2,q4);
addQuestion(2,q5);
addQuestion(2,q6);
addQuestion(3,q7);
addQuestion(3,q8);
addQuestion(3,q9);
}
void Questions::addQuestion(int level, Questions *question)
{
map.insert(pair<int,Questions*>(level,question));
}
Questions* Questions::printQuestion(int level)
{
multimap<int, Questions*>::iterator it;
pair<multimap<int, Questions*>::iterator,multimap<int, Questions*>::iterator> ret;
ret = map.equal_range(level);
if(ret.first != ret.second)
{
size_t sz = distance(ret.first, ret.second);
size_t idx = rand() % sz;
advance(ret.first, idx);
it =ret.first;
return (*it).second;
}
else
{
return NULL;
}
}
答案 0 :(得分:4)
您宣布Questions::Questions()
但未对其进行定义。
是否需要默认构造函数?您使用的是默认构造函数吗?是否需要任何具体实施?
在C ++ 11中,您可以编写
Question() = default;
在类定义中。在C ++ 03中,您可以提供一个空实现:
Question() { }