簡體   English   中英

在C ++中從.h文件訪問類

[英]accessing classes from a .h file in C++

您好,我是在程序中使用頭文件和OPP的新手,我想知道為什么Visual Studio 2010會指出我的代碼中有錯誤。 該代碼可以按需編譯並運行,但是所有對象下都有紅線

這是頭文件

//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);
}

這是我的main()

// 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. 您需要將“主要”功能代碼按字面意義放在主要功能中

     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 )中執行此操作, *.c您決定; 但不要在通常包含的文件中執行此操作。

  4. 不要在頭文件中包含不使用的頭。 這是一個壞習慣,會導致代碼和編譯器膨脹,並不可避免地在以后引起痛苦。 您不應在每個標頭中都包含ctimestdafx ,因為它們沒有引用它。

  5. 您需要將浮在boat.h的“東西” 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 } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM