C++ Funtion/Include Problems.

This is a discussion on C++ Funtion/Include Problems. within the C++ Programming forums, part of the General Programming Boards category; I have done this. When I compile, it tells me [Linker error] undefined reference to `someFunc()' I'm still checking everything ...

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I have done this. When I compile, it tells me

    [Linker error] undefined reference to `someFunc()'

    I'm still checking everything over right now.
    What compiler are you using? You have to make sure all the files are part of your project. In addition the file extensions must be correct: .h for the header file, and .cpp for the file with the definitions.

    Using multiple files is not some alternative to classes. It has to do with program organization. If you had a class, you would organize your program the same way:

    main.cpp:
    Code:
    #include "header.h"
    
    int main()
    {
    	One myone;
    	myone.display();
    
    	Two mytwo(3.2);
    	mytwo.show();
    	
    	return 0;
    }
    header.h:
    Code:
    #ifndef __HEADER_H__
    #define __HEADER_H__
    
    ////CLASS ONE//////
    class One
    {
    private:
    	int num;
    
    public:
    	One(int n);
    	One();
    	void display();
    };
    
    //////CLASS TWO//////
    class Two
    {
    private:
    	double time;
    
    public:
    	Two(double t);
    	Two();
    	void show();
    };
    
    #endif //__HEADER_H__
    definitions.cpp:
    Code:
    #include <iostream>
    #include "header.h"
    
    using namespace std;
    
    ////DEFINITIONS/////
    
    One::One(int n)
    {
    	num = n;
    }
    
    One::One()
    {
    	num = 0;
    }
    
    void One::display()
    {
    	cout<<num<<endl;
    }
    
    Two::Two(double t)
    {
    	time = t;
    }
    
    Two::Two()
    {
    	time = 1.0;
    }
    
    void Two::show()
    {
    	cout<<time<<endl;
    }
    You don't have to organize your code that way. Instead, you can just include everything in one file, which I suggest you do if you are having too much trouble with the multiple file concept. For instance, the same program above contained in a single file:
    Code:
    #include <iostream>
    using namespace std;
    
    ////CLASS ONE//////
    class One
    {
    private:
    	int num;
    
    public:
    	One(int n);
    	One();
    	void display();
    };
    
    //////CLASS TWO//////
    class Two
    {
    private:
    	double time;
    
    public:
    	Two(double t);
    	Two();
    	void show();
    };
    
    //////MAIN//////////
    int main()
    {
    	One myone;
    	myone.display();
    
    	Two mytwo(3.2);
    	mytwo.show();
    	
    	return 0;
    }
    
    ////DEFINITIONS/////
    
    One::One(int n)
    {
    	num = n;
    }
    
    One::One()
    {
    	num = 0;
    }
    
    void One::display()
    {
    	cout<<num<<endl;
    }
    
    Two::Two(double t)
    {
    	time = t;
    }
    
    Two::Two()
    {
    	time = 1.0;
    }
    
    void Two::show()
    {
    	cout<<time<<endl;
    }
    Last edited by 7stud; 04-21-2005 at 08:31 PM.

  2. #17
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Quote Originally Posted by 7stud

    You don't have to organize your code that way. Instead, you can just include everything in one file, which I suggest you do if you are having too much trouble with the multiple file concept. For instance, the same program above contained in a single file:
    This is exactly what I am trying to avoid. The point in this particular project is teaching me to use multiple files. And From what I can see, I know roughly how it works.

    I am useing

    Bloodshed Dev-C++4.9.8.0.

    The name of all files are

    main.cpp, Which contains:

    Code:
    #include <iostream> 
    #include "myfunction.h"
    
    using namespace std; 
    
    int main()
    {
    	someFunc();
    	
    	return 0;
    }
    myfunction.cpp, which contains

    Code:
    #include <iostream>
    #include "myfunction.h"
    
    using namespace std;
    
    void someFunc()
    {
    	cout<<"hello world"<<endl;
    }
    myfunction.h, which contains

    Code:
    //myfunction.h
    #ifndef __MYFUNCTION_H__
    #define __MYFUNCTION_H__
    
    void someFunc();
    
    #endif //__MYFUNCTION
    I don't know what's wrong, other than that linker problem.

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Sorry, I don't know anything about your compiler. If you are getting a linker error, that means Bloodshed can't find one of the files. I would read up on how to add files to a project in Bloodshed. For instance, I can have a file open in my compiler(MS VC++6) along with the other files, and it doesn't mean it's part of the project. I have to specifically insert any new file I create into the project.

    If you can't figure out how to add files to a project, post a new thread titled something like:

    "How the !#$@!#$ can I add files to a project with Bloodshed"

    There should be many people on this forum that can help you with that.
    Last edited by 7stud; 04-22-2005 at 12:59 PM.

  4. #19
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    thanks for the advice.

  5. #20
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    This happens to me when I use DevC++ if I forget to save the source file I use as a header file as a .h file type. Here's what I'd try:
    Code:
    1) Open DevC++.
    2) Click on File->New->Project.
    3) Choose console project of type c++ and name the  project multifilePractice.
    4) Compile the barebones project that comes up and save the file as multifilePractice of File Type .cpp.
    5) Click on File->New->Source File
    6) Add the line: void someFunc(); to the blank file
    7) Compile the project.  
    8) Save the Untitled file using the name myFunctions of File Type .h   
    9) Click on File->New->Source File
    10) Add the lines:
     
    #include<iostream>
    #include "myFunctions.h"
     
    using namespace std;
     
    void someFunc()
    {
      cout << "hello world" << endl;
    }
    to the blank file.
     
    11) Compile the project.
    12) Save the file using the name myFunctions and  using File Type .cpp
    13) Add the line #include myFunctions.h" to mulifilePractice.cpp.
    14) In main() add the line: someFunc(); before the line: system(PAUSE);
    15) Compile and run the project
    16) Add the inclusion gaurds to myFunctions.h as recommended by 7stud
    You're only born perfect.

  6. #21
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    eGads. I am now so very happy. I could wet the bed! WOOHOOOOO!

    Thank you all so much!

    Seems that my happines was meant to be shot down.

    Code:
    //StatCheck.H
    #ifndef STATCHECK_H
    #define STATCHECK_H
    int GetMana(int);
    int GetHP(int);  // Function prototype
    #endif
    Is that kind of code there, ok? Can I declare multiple things such as "GetMana(int)" and "GetHP(int)" as I did there? I can't get it working
    Last edited by Smeep; 04-22-2005 at 05:22 PM.

  7. #22
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Right. Scrap the previous post. I'm still having linker problems, even though I have put them all in the same project.

    Here's the differen't files I have.

    Main.cpp

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <iostream>
    #include "monster.h"
    
    using namespace std;
    
    int main()
    {
     
     int OwnVitality;
     int OwnStrength;
     int OwnInteligence;
     int HitPoints;
     string name;
     
     cout << "Greetings. I am Simpleton. Welcome to this land. \n";
     cout << "And, young traveller, you might be? \n";
     cin >> name;
     cout << "Ah, " << name << "! Yes, That's right, now I remember. \n";
     cout << "Well, There's no time to waste, let's throw you in to your first fight \n";
     
     
       
      int Orc();
    
    
      
      system("PAUSE");	
      return 0;
    }
    statcheck.cpp
    Code:
    // StatCheck.Cpp
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include "monster.h"
    #include "statcheck.h"
    
    using namespace std;
    
    
    int GetStat(intelligence)
    
    {
     
              return intelligence * 2;
            
    }
    statcheck.h
    Code:
    //StatCheck.H
    #ifndef _STATCHECK_H
    #define _STATCHECK_H
    int GetStat(int); // Function prototype
    #endif
    monster.cpp This is the file with the error in, the error reads: "27 C:\Dev-Cpp\CPP_Programs\rpg\monster.cpp
    `GetStat' undeclared (first use this function)"


    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include "statcheck.h"
    using namespace std;
    
    int Orc()
    { 
       
       int vitality = 13;
       int strength = 10;
       int intelligence = 2;
       string name = "Orc";
       int hitpoints;
       string eval;
    
       cout << "You see an " << name << "\n";
       //cout << "Vitality: "  << vitality << "\n"; 
       //cout << "Strength: " << strength << "\n";
       //cout << "Intelligence: " << intelligence << "\n";
       cout << "Do you wish to evaluate the " << name << "? \n";
       cout << "Y or N? \n";
       cin >> eval;   
       if (eval == "Y")
        {
            cout << "Stats: " cout << "Stats: " << GetStat(intelligence) << "\n"; THE ERROR IS HERE
           
        }
    
     
        //wait until user is ready before terminating the program
        // to all user to see the program results
        //system("pause");
        //return 0;
    }
    monster.h
    Code:
    //Monster.H
    
    #ifndef _MONSTER_H
    #define _MONSTER_H
    int Orc();  // Function prototype
    #end
    Oddly enough, "int orc()" works fine.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    int GetStat(intelligence)
    Shouldnt this be:
    int GetStat(int intelligence)
    ?

    Also, in monster.h, you should use #endif instead of #end

    Post your compile log.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    In monster.h, it IS #endif, I just copied it wrong.

    Should it be (int intelligence) if intelligence is already declared? (edit - It should be "mana" there, intelligence is already defined as 2, and mana is derived from intelligence)

    Compile log - Is that the part at the bottom? with the errors in? Or some kind of externally created file?

    EDIT: I found the compile log
    Code:
    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\CPP_Programs\rpg\Makefile.win"
    Executing  make...
    make.exe -f "C:\Dev-Cpp\CPP_Programs\rpg\Makefile.win" all
    g++.exe -c monster.cpp -o monster.o -I"C:/Dev-Cpp/include/c++"  -I"C:/Dev-Cpp/include/c++/mingw32"  -I"C:/Dev-Cpp/include/c++/backward"  -I"C:/Dev-Cpp/include"  
    
    monster.cpp: In function `int Orc()':
    monster.cpp:17: `GetStat' undeclared (first use this function)
    monster.cpp:17: (Each undeclared identifier is reported only once for each 
       function it appears in.)
    
    make.exe: *** [monster.o] Error 1
    
    Execution terminated
    Last edited by Smeep; 04-23-2005 at 04:56 PM.

  10. #25
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You need to get a C++ book, and look at the required elements for a function definition:

    1) return type
    2) function name
    3) if any parameters, then a list of parameters plus their types
    4) function body

    So, in your function definition here:
    Code:
    int GetStat(intelligence)
    
    {
     
              return intelligence * 2;
            
    }
    you are missing one of the required elements.

    Using Functions:

    Here is a simple example of a function:
    Code:
    void display(int n)
    {
    	cout<<n<<endl;
    }
    All functions require a return type. In the display() function above, the return type is 'void', which stands for 'nada', 'nothing', 'zip'. That means, the function doesn't return anything. Functions also can have things called 'parameters'. The parameter in the function above is 'int n'. That means the function is expecting any integer to be sent to it. The integer value that is sent to the function will be stored in the variable 'n'.

    Here is what the function looks like as part of a program:

    Code:
    #include <iostream> //contains cout
    
    using namespace std; 
    //makes it so you don't have to write 'std::cout'
    //instead you can just write 'cout'
    
    
    //function:
    void display(int n)
    {
    	cout<<n<<endl;
    }
    
    
    int main()
    {
    	int num = 10;
    	display(num);
    
    	return 0;
    }
    In main(), the function is called by this line:

    display(num);

    That instructs the program to send the value contained in the variable 'num' to the function named display(), and then execute the code in the function. Since num is equal to 10, 10 is sent to the display() function. The value 10 is then stored in the variable 'n', which is the function parameter. Then, the code in the function is executed. After the last line in the function is executed, execution returns to the place in main() where the function was called, and then the statements thereafter in main() are executed.

    Here is another example of a function:
    Code:
    double multiplyBy3(int x)
    {
    	double temp = x * 3.0;
    	return temp;
    }
    This time the return type is double(a double is a number with a decimal point like 2.5). The return type has to be specified as 'double' because the function returns a double value in this line:

    return temp;

    The multiplyBy3() function also has an int parameter, so it must be sent an int.

    Adding the multiplyBy3() function to the previous program looks like this:

    Code:
    #include <iostream> //contains cout
    
    using namespace std; 
    //makes it so you don't have to write 'std::cout'
    //instead you can just write 'cout'
    
    double multiplyBy3(int x)
    {
    	double temp = x * 3.0;
    	return temp;
    }
    
    void display(int n)
    {
    	cout<<n<<endl;
    }
    
    
    int main()
    {
    	int num = 10;
    	display(num);
    
    	int number = 5;
    	double result = multiplyBy3(number);
    	
    	cout<<result<<endl;
    
    	return 0;
    }
    The function multiplyBy3() is called in main() by this line:

    double result = multiplyBy3(number);

    The function is sent the int value stored in number. Since number is equal to 5, 5 is sent to the multiplyBy3 function, and then 5 is stored in the variable 'x', which is the function parameter. Then, the code in the function is executed. When the return statment is executed:

    return temp;

    the function gets the value in temp, and sends it back to main(). Since temp equals 15.0, 15.0 is sent back to main(). When a function returns a value, the function call is replaced by the return value. So, this line in main():

    double result = multiplyBy3(number);

    becomes:

    double result = 15.0;

    Then, the line:

    cout<<result<<endl;

    displays the return value. Note that you cannot do this to display 'result':

    display(result);

    The display() function requires an int to be sent to it, not a double, and result is type double. Therefore, you would get an error if you tried that: the types have to match.
    Last edited by 7stud; 04-23-2005 at 05:19 PM.

  11. #26
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    I don't understand what that has to do with why my compiler won't recognise the function that I have?
    I'm using Dev-C++ 4.9.8.0

  12. #27
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Is that kind of code there, ok? Can I declare multiple things such as "GetMana(int)" and "GetHP(int)" as I did there?
    Yes. However, to make it less confusing for you, I would make sure my function declaration/prototype is exactly the same as the first line of your function definition--copy and paste it to be absolutely sure.

    This is the file with the error in, the error reads: "27 C:\Dev-Cpp\CPP_Programs\rpg\monster.cpp
    `GetStat' undeclared (first use this function)"
    There shouldn't be an error there, IF you named your files correctly. However, looking at your code, I consider that suspect:

    monster.cpp :

    Code:
    #include "statcheck.h"
    statcheck.h

    Code:
    //StatCheck.H
    #ifndef _STATCHECK_H
    #define _STATCHECK_H
    int GetStat(int); // Function prototype
    #endif
    It doesn't matter what the file name is, but the exact filename has to be in the include:
    Code:
    #include "statcheck.h"
    Last edited by 7stud; 04-23-2005 at 05:32 PM.

  13. #28
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I don't understand what that has to do with why my compiler won't recognise the function that I have?
    It doesn't, but once you get the file thing straightened out, that will be your next problem.

  14. #29
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    It is exactly the same.
    I'm using Dev-C++ 4.9.8.0

  15. #30
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) Is the include file part of the project?

    2) I'm pretty sure it's mandatory that you have to include monster.h in monster.cpp--that was one of the first things we discussed in this thread. I already discussed why you need to include the .h file in main(): it's just like declaring a variable before using it. However, the linker also needs the function declaration(.h file) and the function definition(.cpp file) together in one file(the .cpp file including the .h file) to sort things out. It's the one part of the whole multiple file scenario that doesn't really make sense, i.e. you have to include the .h file for the function in main(), and you have to again include the .h file for the function in the .cpp file for the function.

    See if that works.

    As a side note, this including stuff gets very complicated as the number of files increases because functions in one file might call functions in other files that may call functions in yet other files. That is why you put those inclusions guards around .h files. If things get too confusing as to what .h files need to be included in a certain.cpp file--just add every .h file in your program to the .cpp file, and the inclusion guards will make sure you don't add the .h file more than once.
    Last edited by 7stud; 04-23-2005 at 07:52 PM.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM

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