2012-12-18 51 views
0

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?錯誤在我的頭和.cpp文件

嘿我在我的遊戲收到此錯誤。我有一個標題和.cpp文件的問題,所以它顯然在那。但我一直在看通過頭和.cpp文件,我找不到問題。以下是錯誤:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Questions::Questions(void)" ([email protected]@[email protected]) referenced in function "void __cdecl `dynamic initializer for 'questions''(void)" ([email protected]@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

@Luchian:解精設置是q爲重複,因爲它是題外話現代SO(要求列出原因,而不是一個簡單的答案)。用足夠勤奮的mods將會被刪除 –

回答

4

你聲明Questions::Questions()但沒有定義它。

它需要一個默認的構造函數嗎?你使用默認的構造函數嗎?它是否需要任何特定的實現?

在C++ 11,你可以寫

Question() = default; 
類定義

。在C++ 03,您可以提供空的實現:

Question() { }