2014-03-31 19 views
-1

你好我是在我的程序中使用頭文件和OPP的新手段,我想知道爲什麼Visual Studio 2010會說我的代碼中有錯誤。該代碼可以編譯和運行根據需要,但也有下的所有對象訪問C++中的.h文件中的類

的紅線這裏是頭文件

//functions.h 
#include "stdafx.h" 
#include <iostream> 
#include <ctime> 
#include <string> 

using namespace std; 

class vehicle 
{ 
public: 
int hp; 
int ammo; 
void displayHP(); 
void displayAmmo(); 
void displayName(); 
string vesselName; 
void setName(string); 
void moveUP(int& y); 
void moveDown(int& y); 
void moveLeft(int &x); 
void moveRight(int &x); 
private: 
}; 
//implementation section 
void vehicle::displayHP(){cout<<hp<<endl;} 
void vehicle::displayAmmo(){cout<<ammo<<endl;} 
void vehicle::setName(string name){vesselName=name;} 
void vehicle::displayName(){cout<<vesselName<<endl;} 
void vehicle::moveUP(int& y) 
    { 
     y=y-1;//moves boat up 
     system("cls"); 
    } 
void vehicle::moveDown(int& y) 
{ 
     y=y+1;//moves boat down 
     system("cls"); 
} 
void vehicle::moveLeft(int &x) 
{ 
     x=x-1;// moves the boat left 
     system("cls");  
} 


void vehicle::moveRight(int &x) 
{ 
    x=x+1;//moves boat right 
    system("cls"); 
} 
void moveBoat(int &x,int& y, int a,int b,int s,int turn) 
{ 

這裏是包含船運動的頭文件。該程序編譯細而設計的,但我很困惑,爲什麼Visual Studio是聲稱有這麼多的錯誤,我添加的行評價,其中在boat.h文件

//boat.h 
#include "stdafx.h" 
#include <iostream> 
#include <ctime> 
#include <string> 
#include "functions.h" 

using namespace std; 

void moveBoat(int &x,int& y, int,int,int,int); 
void youScrewedUp(int &x,int& y, int,int,int,int); 



int movement=0; 

vehicle destroyer; 
    destroyer.hp=500;//<==== getting a red line under the word destroyer error says "this deceleration has no storage class or type specifier" 
    destroyer.ammo=500;//<==== getting a red line under the word destroyer error says "this deceleration has no storage class or type specifier" 



displayArray(x,y,a,b,s);//<===="this deceleration has no storage class or type specifer" 
destroyer.setName("USS YAY I LEARNED CLASSES");//<===="this deceleration has no storage class or type specifer" 
destroyer.displayName();//<===="this deceleration has no storage class or type specifer" 
destroyer.displayHP();//<===="this deceleration has no storage class or type specifer" 
cout<<"Boat Location X "<<x<<" Y "<<y<<endl; 
if(s==1) 
{ 
cout<<"ENEMY SHIP SIGHTED"<<endl;//<===="this deceleration has no storage class or type specifer" 
} 
cout<<"1.move left"<<endl;//<===="this deceleration has no storage class or type specifer" 
cout<<"2.move right"<<endl;//<===="this deceleration has no storage class or type specifer" 
cout<<"3.move up"<<endl;//<===="this deceleration has no storage class or type specifer" 
cout<<"4.move down"<<endl;//<===="this deceleration has no storage class or type specifer" 
cin>>movement;<===="this deceleration has no storage class or type specifer" 

    switch(movement)//<==expected a deceleration 
    { 
    case 1: 
     if(x>0)//does not allow boat to leave grid 
     { 
      destroyer.moveLeft(x); 
     } 
     else 
     { 
      youScrewedUp(x,y,turn,a,b,s);// function that repeats the movement function and displays a message 
     } 
     break; 

     case 2: 
     if(x<9)//boundary 
     { 
      destroyer.moveRight(x); 
     } 
     else 
      { 
      youScrewedUp(x,y,turn,a,b,s); 
      } 
     break; 

     case 3: 
     if(y>0)//boundary 
     { 
      destroyer.moveUP(y); 
     } 
     else 
      { 
      youScrewedUp(x,y,turn,a,b,s); 
      } 
     break; 

     case 4: 
     if(y<9)//boundary 
     { 
      destroyer.moveDown(y); 
     } 
     else 
      { 
      youScrewedUp(x,y,turn,a,b,s); 
      } 
     break; 


    } 
    turn++;//adds one to the turn counter to cycle to the enemies turn 



} 

void youScrewedUp(int &x, int &y,int turn, int a, int b,int s)// must pass the x y values by refferance 
{ 
cout<<"where are you going your leaving the battlefield"<<endl; 
      cout<<"please make another selection"<<endl; 
      system("pause"); 
      system("cls"); 
      moveBoat(x,y,turn,a,b,s); 
} 

這裏的錯誤是我的主要工作()

// arrayTest.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include "array.h" 
#include "boat.h" 
#include "enemy.h" 
#include <iostream> 
#include <ctime> 

using namespace std; 

int main() 
{ 
int turn=1;// turn counter cycles between 1 and 2 during player and enemy turns 
int x=7;//x coordinate for destroyer 
int y=6;//y coordinate for destroyer    
int a=3;//x coordinate for sub 
int b=4;//y coordinate for sub 
int s=0;//toggle for submerged or not chose not to use a bool because I used a random number generator to allow a 25% chance opposed to a 50% chance 
srand (time(NULL)); 

do{ 
    moveBoat(x,y,turn,a,b,s);//function that moves the boat 
    moveEnemy(a,b,turn,x,y,s);//function to move the enemy 
}while(turn<3);// this is temporary will be replaced when a hp and weapons system is completed 
    system("pause"); 
return 0; 
} 
+1

你需要把你的代碼'主()' – WileTheCoyot

+1

你說MSVC內接受此代碼? –

+0

你應該閱讀一本關於C++的書。因爲你所做的一切都不好。可能是[那些]之一我只是張貼我的main()在原始帖子的底部,我相信(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Rosme

回答

4

這超出了您的基本問題,並添加了一些其他的東西,這些東西將改善您的代碼並希望能夠理解。

  1. 你需要從字面上把你的「主」的功能代碼在主函數

    int main(int argc, char * argv[]) 
    { 
        //do stuff here.... 
        return 0; 
    } 
    
  2. 你應該包括頭部防護,以防止你包括「function.h」多次。我也強烈建議將它重命名爲Vehicle.h,以表示它正在提供的類。

    #ifndef __VEHICLE_H__ 
    #define __VEHICLE_H__ 
    //... all the good stuff. 
    
    #endif 
    
  3. 我強烈建議你從你的頭文件中刪除using namespace std,因爲這樣做將垃圾人誰願意使用你的頭的命名空間。在需要的地方簡單地使用std::,或者如果您不想在任何地方使用它們,請考慮爲您正在使用的特定功能執行using std::xyz;。這種方式至少可以在以後追溯碰撞。如果你想在一個實現文件(即*.c)中這樣做,但不要在通常包含的文件中進行。

  4. 請勿在頭文件中包含您未使用的頭文件。這是一個壞習慣,導致代碼和編譯器膨脹,並且不可避免地會導致以後的痛苦。您不應在每個標題中包含ctimestdafx,因爲它們不會引用它。

  5. 你需要把身體的「東西」是浮動的內部boat.h成函數

    //somefunct 
    void somefunction() 
    { 
        int movement=0; 
    
        vehicle destroyer; 
        destroyer.hp=500;//<==== getting a red line under the word destroyer error says "this deceleration has no storage class or type specifier" 
        destroyer.ammo=500;//<==== getting a red line under the word destroyer error says "this deceleration has no storage class or type specifier" 
    
    
        //.... Everything else 
    
    } 
    
+0

我很抱歉......這個問題是完全沒有必要的,我不小心複製功能的functions.h頭文件的一半,並留下了一些落後......我浪費時間anybodies不 –

+1

完全是浪費道歉,每一件事情一樣,我說仍然適用。瞭解什麼是你可以從:-) – UpAndAdam

+0

是每一次經歷,並感謝您的意見...我真的不知道爲什麼,我試圖轉移到應用到boat.h所有代碼反正functions.h文件。 ..我認爲我是在爲線下遺產而努力,比如傷害和攻擊。我會刪除命名空間std;從程序,但它將是一個類最終使用命名空間標準;是一個課程要求......你能否啓發我,告訴我爲什麼這是不好的做法? –