Whats wrong with this code?!

This is a discussion on Whats wrong with this code?! within the C++ Programming forums, part of the General Programming Boards category; Can some one please help me with this code, and explain why it doesn't work? (I'm using c++ builder 6 ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    180

    Question Whats wrong with this code?!

    Can some one please help me with this code, and explain why it doesn't work?
    (I'm using c++ builder 6 by Borland)

    Code:
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    
    #pragma hdrstop
    #include "Tank1.h"
    #include "Unit1.h"
    #include "Global_Var.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    
     int x;
     int y ;
     Tank1 *Tank = new Tank1;
     TImage *Sprite_Tank1 = new TImage(Owner);
    
    
    
    //extern TImage *Sprite_Tank1 ;
    
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    
    
    }
    //---------------------------------------------------------------------------
    
    void WTF(int x, int y,Tank1 *Tank, TImage *Sprite_Tank1)
    {
    
    int temp;
    
    
    temp=0;
    
    temp = temp + 2;
    
    
    Tank->body = blue;
    Sprite_Tank1->Picture->LoadFromFile("Cheshire.ico");
    
    Form1->Canvas->Draw((x),(y),Sprite_Tank1->Picture->Graphic );
    
    }
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    
    
    int i;
    
    y=200;
    x=100 ;
    
    
    
    }
    //---------------------------------------------------------------------------
    
    
    
    //---------------------------------------------------------------------------
    
    
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    Timer1->Enabled = true;
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
    static int y ;
    static int x;
    y=y+10;
    x=x+10;
    WTF(x,y,Tank,Sprite_Tank1);
    Form1->Repaint();
    }
    //---------------------------------------------------------------------------
    this is in my main .cpp file

    this is in my Tank1.h file

    Code:
    #include <vcl.h>
    
    enum color {blue, black, green, camo};
    
    class Tank1
    {
    public:
            TGraphic *Sprite_Tank1;
            color body;
            int x1;
            int y1;
    
           // Move_left();
          //  Move_right();
          //  Shoot();
    
    
    };
    and this is my Global_Var.h file
    Code:
     //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    
    
    
    extern int x;
    extern int y ;
    extern Tank1 *Tank = new Tank1;
    //extern TImage *Sprite_Tank1 = new TImage(Owner);
    I get this error when I compile:

    [C++ Error] Unit1.cpp(17): E2451 Undefined symbol 'Owner'

    thanks for the help


    DW

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Few issues here:
    Code:
    extern Tank1 *Tank = new Tank1;
    You should never make variable assignments in headers. You shouldn't be allocating memory either. Also, you shouldn't be making an assignment to an extern variable here, even if it was "OK" to make assignments.

    The keyword "extern" means that there's a variable some place in another file, and you're providing a hook or link to it, so that other files can reference it. In short, this line is just all together wrong.

    [edit]
    Code:
    Tank1 *Tank = new Tank1;
     TImage *Sprite_Tank1 = new TImage(Owner);
    As of here, there is no instance called "Owner" any place, so you cannot pass it as an argument, because it doesn't exist.
    [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Where's Owner defined? I can't seem to see it anywhere in your code apart from the places where you use it.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WDT
    Where's Owner defined? I can't seem to see it anywhere in your code apart from the places where you use it.
    I think their problem, in their mind or mental image of what's going on in their code, comes from this:
    Code:
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
    Here they have, or would have, created Owner, in that it's an argument for this function. But they've got ahead of themselves here:
    Code:
    TImage *Sprite_Tank1 = new TImage(Owner);
    Thinking either that:
    a) Owner exists as an entity already.
    b) Owner exists because it's being defined.

    In either case, they're wrong. And this is what happens when you rely on globals.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    okydoky I gotcha. when i put that in the button it worked, because owner was declared, BUT, anywehre else, ower doesn't get declared, and hence no compile! excellent. Must find a way around this now..... thx for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21