Functions in class and class variables.

This is a discussion on Functions in class and class variables. within the C++ Programming forums, part of the General Programming Boards category; The class doesn't sees the function " place(); " in the main.cpp and the int x and int y is ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Functions in class and class variables.

    The class doesn't sees the function "place();" in the main.cpp and the int x and int y is also not recognized when using it in the functions owned by the class:

    Code:
    #ifndef ORGAN_H
    #define ORGAN_H
    
    class Organ
    {
    public:
        int x;
        int y;
     
        void kill(int a,int b);
        void it_lives(int prop);
        void SetCoord(int x, int y);
    
    };
    
    #endif // ORGAN_H
    Code:
    #include "organ.h"
    
    void SetCoord(int x, int y)
    {
        place(x,y,"@");//ERROR: Undeclared
    }
    void kill(int a,int b)
    {
        x=a;//ERROR: x and y undefined
        y=b;
        place(x,y," ");
    }
    void it_lives(int prop)
    {
        if(prop==0)
        {
            kill(x,y);
        }
    }
    place(); is defined in the main.cpp file. The header file is defined in the main.cpp.
    Last edited by Karakus; 01-15-2006 at 01:32 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    void SetCoord(int x, int y)
    {
    place(x,y,"@");//ERROR: Undeclared
    }
    You need the class name as well
    Code:
    void Organ::SetCoord(int x, int y)
    {
        place(x,y,"@");//ERROR: Undeclared
    }
    > The class doesn't sees the function "place();"
    Then prototype it in a header file, and include that header file in organ.cpp

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    I made a new header, "functions.h", but so, the functions were also needed by the main.cpp and I included it also to the main.cpp, now I get:
    The Build log:
    .objs\main.o:main.cpp.text+0x100): multiple definition of `gotoxy(int, int)'
    .objs\organ.organ.cpp.text+0x100): first defined here
    .objs\main.o:main.cpp.text+0x13e): multiple definition of `place(int, int, std::string)'
    .objs\organ.organ.cpp.text+0x13e): first defined here

    The Build message:
    #warning: This file includes at least one deprecated or antiquated header. Line 32.
    In line 32 there is just a struct definition, no problem there.

    I don't have any duplications of these functions, what's wrong?
    Thanks.
    Last edited by Karakus; 01-15-2006 at 02:01 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    That's what happens when you put code inside header files.

    My guess, your functions.h has
    Code:
    void gotoxy ( int x, int y ) {
      // something from the FAQ?
    }
    Rather than having
    void gotoxy ( int x, int y );

    And in functions.cpp
    Code:
    #include "functions.h"
    void gotoxy ( int x, int y ) {
      // something from the FAQ?
    }
    > #warning: This file includes at least one deprecated or antiquated header. Line 32.
    You have something like
    #include <iostream.h>

    You should remove the .h from all your C++ header files, like this
    #include <iostream>

    Also, C headers included in C++ programs were written as
    #include <stdlib.h>
    Which would now be
    #include <cstdlib> // prepend c, and lose the .h

    Finally, after all the includes, add
    using namespace std;

    Eg.
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    // rest of C++ code

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Ok, thanks .
    I added the whole function to the functions.h file (I didn't know what a prototype was), and the deprecation was string.h and iostream.h.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Class and functions
    By Rune Hunter in forum C++ Programming
    Replies: 15
    Last Post: 08-05-2005, 09:17 PM

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