2011-12-12 34 views
2

當我嘗試聲明我的類「遊戲」的實例時,收到編譯錯誤「error:'遊戲'沒有爲main.cpp命名類型」。錯誤:'x'未命名類型

如果可能無關緊要,但我使用的是代碼塊。從Game.cpp

相關代碼Main.cpp的

#include "../include/main.h" 

class Game 
{ 
    private: 

    public: 
}; 

相關代碼

#include "../include/main.h" 

Game g; //this is the line it is referring to 

int main(int argc, char* args[]) 
{ 
    return 0; 
} 

我纔剛剛開始學習C++,所以我可能忽略了一些東西明顯:(

+0

在你Main.cpp的頭文件,不包括類「遊戲」的定義,所以你應該像Game.h一個文件中定義類遊戲,和在你的Main.cpp中添加#include「Game.h」 –

回答

3

在標題中包含「遊戲」聲明

notepad main.h =>

#ifndef MAIN_H 
#define MAIN_H 

class Game 
{ 
    private: 
     ... 
    public: 
     ... 
}; 
#endif 
// main.h 

記事本的main.cpp =>

#include "main.h" 

Game g; // We should be OK now :) 

int 
main(int argc, char* args[]) 
{ 
    return 0; 
} 

gcc -g -Wall -pedantic -I../include -o main main.cpp 

注意你如何:

1)定義你的類(在頭的任何類型定義,常量等)沿

2)#include需要這些定義的任何.cpp文件中的頭文件

3)編譯時使用「-I」指定目錄ectory(或目錄)包含你的頭

「希望幫助

0

也許你可以刪除遊戲類聲明中Game.cpp並嘗試創建一個名爲‘Game.h’下../inclue/另一個文件如:

#ifndef _GAME_H_ 
#define _GAME_H_ 

class Game 
{ 
    private: 
    public: 
}; 

#endif 

並將此Header文件包含在Main.cpp中。然後我認爲錯誤不會發生:)

因爲我們通常使用.cpp文件作爲類定義,.h文件作爲聲明,所以在Main.cpp中包含.h文件。

0

C文件或cpp文件是發生多重錯誤發生的問題。

每個

# pragma once 
Or 
# Ifndef __SOMETHING__ 
# define __SOMETHING__ 

Add the code ... 

# Endif