在C ++中从.h文件访问类

时间:2014-03-31 20:20:06

标签: 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);
}

这是我的主要()

// 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 个答案:

答案 0 :(得分:4)

这超出了您的基本问题,并添加了一些其他可以改善您的代码并希望了解的内容。

  1. 您需要将'main'功能代码直接放在主函数

    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
    
    }