Like Tree1Likes
  • 1 Post By laserlight

Undefined reference to struct

This is a discussion on Undefined reference to struct within the C++ Programming forums, part of the General Programming Boards category; edit: gotcha! forgot to add #include <string> to the header and to compile the two src together. Good afternoon. I'm ...

  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question Undefined reference to struct

    edit: gotcha!

    forgot to add #include <string> to the header and to compile the two src together.


    Good afternoon. I'm getting the error mentioned in the title when I try to pass a struct from golfers which was dinamically created.

    Code:
     
    #include <iostream> 
    #include <string>
    #include <cstring>
    #include "../Headers/golf.h"
    
    int main() 
    { 
       using std::cout; 
       using std::cin; 
       using std::strlen;
       using std::string; 
       using sport::setgolf;
       using sport::golf; 
       
       string fullname;
       int players, handicap; 
       
       cout << "How many players are playing? "; 
       while(!(cin >> players))
       { 
         cin.clear();
         while(cin.get() != '\n') 
           continue; 
         cout << "Please enter how many players are playing: ";     
       }        
       cin.get();
       golf *golfers = new golf[players];
       for(int i = 0; i < players; i++) 
       { 
          cout << "Enter your full name: "; 
          if(cin.get() == '\n') 
           break; 
          getline(cin, fullname);
          cout << "Enter the handicap: "; 
          (cin >> handicap).get();
          setgolf(golfers[i], fullname, handicap);
       } 
       delete [] golfers;
    }
    Code:
     
     36: referência indefinida para `sport::setgolf(sport::golf&, std::string, int)'
    Code:
     
    #include <iostream> 
    #include <string>
    #include <cstring>
    #include "../Headers/golf.h"
    
    namespace sport { 
        
      void setgolf(golf &g, const std::string name, int hc)
      { 
         g.fullname = name;
         g.handicap = hc;      
      }
      
      int setgolf(golf &g)
      { 
        using std::string;
        using std::cout;
        using std::cin; 
        using std::strlen;
        
        cout << "Please enter full name: "; 
        cin >> g.fullname;
        cout << "Please enter handicap: ";
        cin >> g.handicap;  
        if(g.fullname.length() == 0)
         return 0; 
        else 
         return 1;    
      }
      
      void handicap(golf &g, int hc) 
      { 
        g.handicap = hc;  
      }     
      
      void showgolf(const golf &g)
      { 
         using std::cout; 
         using std::endl; 
         
         cout << "Your name: " <<  g.fullname << endl;   
         cout << "Handicap: " << g.handicap << endl; 
      }                           
    }
    Code:
     
    #ifndef GOLF_H_
    #define GOLF_H_
    
    const int LEN = 40; 
    
    namespace sport { 
    
      struct golf { 
        
        std::string fullname; 
        int handicap;    
      };     
    
        // non-interactive version: 
        // function sets golf structure to provided name, handicap
        // using values passed as arguments to the function
        void setgolf(golf & g, const std::string name, int hc);
        // interactive version:
        // function solicits name and handicap from user
        // and sets the members of g to the values entered
        // returns 1 if name is entered, 0 if name is empty string
        int setgolf(golf & g);
        // function resets handicap to new value
        void handicap(golf & g, int hc);
        // function displays contents of golf structure
        void showgolf(const golf & g);
    }    
    
    #endif
    Last edited by thames; 12-20-2012 at 07:36 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Please use std::vector instead of new [].
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Elysia View Post
    Please use std::vector instead of new [].
    Since he is probably now learning, I would suggest not. Focus on learning the concepts of OOP first thames
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Please use std::vector instead of new [].
    I would use std::array. Is that ok? but why?

    hey std! long time no see you!

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Of course it is ok when you are on your first steps. Focus on the concepts of OOP

    Elysia knows, of course what she says, but the please at the start of her post, made me post
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by std10093 View Post
    Since he is probably now learning, I would suggest not. Focus on learning the concepts of OOP first thames
    Focus on learning how to program C++ properly!
    Don't use new unless absolutely necessary.

    Quote Originally Posted by thames View Post
    I would use std::array. Is that ok? but why?

    hey std! long time no see you!
    std::array won't work for dynamic arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Elysia View Post
    std::array won't work for dynamic arrays.
    I read array instead of std::array thames! I agree with Elysia on that
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    Quote Originally Posted by std10093
    Since he is probably now learning, I would suggest not. Focus on learning the concepts of OOP first
    std10093, I suggest that you read Learning Standard C++ as a New Language by Bjarne Stroustrup, the designer and original implementer of C++.
    Elysia likes this.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference
    By Dudealadude in forum C Programming
    Replies: 1
    Last Post: 11-29-2011, 01:43 PM
  2. : undefined reference to `pow'
    By mohitsaxena019 in forum C Programming
    Replies: 13
    Last Post: 07-25-2010, 08:35 AM
  3. Undefined reference
    By TheEngineer in forum C Programming
    Replies: 17
    Last Post: 08-12-2009, 10:34 AM
  4. Undefined reference?
    By Tirith in forum C Programming
    Replies: 8
    Last Post: 08-11-2009, 02:39 PM
  5. undefined reference to
    By shaun84 in forum C Programming
    Replies: 12
    Last Post: 10-15-2006, 02:32 AM

Tags for this Thread


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