-1

Hello I am new to using header files and OPP in my programs and I am wondering why visual studio 2010 is stating that there are errors in my code. The code compiles and runs as desired but there are red lines under all of the objects

here is the header file

//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)
{

here is the header file that contains the boats movements. The program compiles fine and works as designed but I was confused why visual studio is claiming there are so many errors I added line comments to where the errors in the boat.h file

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

Here is my 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;
}
John Snow
  • 87
  • 11
  • 1
    you need to put your code inside the `main()` – WileTheCoyot Mar 31 '14 at 20:22
  • 1
    Did you say MSVC accepted this code? –  Mar 31 '14 at 20:28
  • You should read a book about C++ instead. Because what you do is not good at all. Possibly one of [those](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Rosme Mar 31 '14 at 20:28
  • I just posted my main() on the bottom of the original post I believe i have properly declared the variables and functions within the classes I could be wrong seeing as I am new to this and that is why I am asking for the wisdom of stack overflow... – John Snow Mar 31 '14 at 20:29
  • @faranwath yes It compiles fine and runs the way it should in visiual studio 2010 It is just throwing red lines and all sorts of other wierdness at me in the editor Its bothering me because typically with those types of error notifications in the editor it will not compile. – John Snow Mar 31 '14 at 20:30
  • @JohnSnow: Nope, that cannot compile. The contents of `boat.h` render this program illegal. –  Mar 31 '14 at 20:32
  • 1
    you need to put the content in `boat.h` inside a function – WileTheCoyot Mar 31 '14 at 20:33
  • @faranwath.... I can send you a screenshot it compiles – John Snow Mar 31 '14 at 20:36
  • @JohnSnow: Are you entirely, positively sure that you compiled the program you showed us? –  Mar 31 '14 at 20:43

1 Answers1

4

This goes beyond your base question and adds some other things that will improve your code and hopefully understanding.

  1. You need to put your 'main' function code literally in a main function

    int main(int argc, char * argv[])
    {
        //do stuff here....
        return 0;
    }
    
  2. You should include header guards to prevent you from including 'function.h' multiple times. I would also strongly suggest renaming it to Vehicle.h to be emblematic of the class it is providing.

    #ifndef __VEHICLE_H__
    #define __VEHICLE_H__
    //... all the good stuff.
    
    #endif
    
  3. I would STRONGLY suggest you remove using namespace std from your header file as doing so will trash the namespaces of anyone who wishes to use your header. Instead simply use std:: where needed or if you really don't want to use them everywhere, consider doing a using std::xyz; for the specific features you are using. This way at least you can trace down collisions later. If you want to do this in an implementation file (i.e. *.c) that's up to you; but don't do it in files that are included generally speaking.

  4. Don't include headers you aren't using in your header file. This is a bad habit, leads to code and compiler bloat, and will inevitably cause pain later. You shouldn't be including ctime or stdafx in each of your headers as they don't refer to it.

  5. You need to put the body of 'stuff' that is floating inside boat.h into a function

    //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
    
    }
    
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
  • I apologize... this question was absolutely not necessary I accidentally copied half of a function to the functions.h header file and left some behind... I apologize for wasting anybodies time – John Snow Mar 31 '14 at 20:44
  • 1
    Not a total waste, every thing else I said still applies. Learn what you can from every experience :-) – UpAndAdam Mar 31 '14 at 20:47
  • Yes and thank you for the advice... I don't really know why I was trying to move to the functions.h file all the code applied to the boat.h anyway... I think I was doing it for down the line inheritance things like damage and attacks. I would remove the namespace std; from the program but it is going to be for a class final where using namespace std; is a class requirement... could you enlighten me and tell me why it is bad practice? – John Snow Mar 31 '14 at 20:50
  • using namespace std is fine in your `main` but it is horrible in a header file, and anyone who tells you otherwise is suspect. Honestly I tend to imagine you misunderstand the requirement. The problem is this: let's say I define a method called `xyz` and use it in a header and implementation of my own. Then you want to include my stuff into your project. If `xyz` existed in the std namespace you'd have a collision. This brings up all sorts of other problems that you should search or ask your teacher about. – UpAndAdam Mar 31 '14 at 20:54
  • http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – UpAndAdam Mar 31 '14 at 20:54
  • +1 for making the effort to salvage the unsalvageable. – Moo-Juice Mar 31 '14 at 21:03