Find the answer to your Linux question:
Results 1 to 4 of 4
Hi All, I'm getting this identicaly error (obviously with different file names and classes shown) for every file I try to compile right now that has #include <string> This particular ...
  1. #1
    Linux Guru jmadero's Avatar
    Join Date
    Jul 2007
    Location
    California
    Posts
    1,958

    Same Error for All Compiling (String)

    Hi All,

    I'm getting this identicaly error (obviously with different file names and classes shown) for every file I try to compile right now that has #include <string>

    This particular one is an example that is actually just trying to compile an example from the book I'm using:

    Code:
    fig03_17.cpp:(.text+0x45): undefined reference to `GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
    Thought it was me so I spent hours yesterday trying to clean basic code up and ultimately failed, finally decided to just try to compile the cpp that is included in chapter 3 of my book that I am using to learn C++. Any ideas??


    Here's an idea of my code, really really basic:

    Account.h

    Code:
    //Chapter 3, Accounts Exercise: Accounts.h
    //Presents the public interface of the class
    //Member function definitions appear in Accounts.cpp
    
    
    #include <string>
    using namespace std;
    
    class Account
    {
    public:
    	Account ( string );
    	void setAccountName ( string );
    	string getCourseName ();
    private:
    	string accountName;
    };

    Account.cpp

    Code:
    //Chapter 3, Accounts Exercise: Accounts.cpp
    //Implementations of the Accounts member-function definitions.
    //Some functions perform validations
    
    
    #include <iostream>
    #include "Account.h"
    using namespace std;
    
    Account::Account (string name)
    {
    	setAccountName (name);
    }
    
    void Account::setAccountName (string name)
    {
    	accountName = name;
    }
    
    string Account::getAccountName
    {
    	return accountName;
    }

    Main.cpp

    Code:
    //Chapter 3, Accounts Exercise: Main.cpp
    //Create and manipulate Account object
    
    #include <iostream>
    #include "Account.h"
    using namespace std;
    
    int main()
    {
    	Account myAccount("Test");
    }
    Last edited by jmadero; 11-24-2011 at 04:42 PM.
    Bodhi 1.3 & Bodhi 1.4 using E17
    Dell Studio 17, Intel Graphics card, 4 gigs of RAM, E17

    "The beauty in life can only be found by moving past the materialism which defines human nature and into the higher realm of thought and knowledge"

  2. #2
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,301
    Apparently, this is a linker problem and it should be solvable by adding the file with the gradebook class to your project. How you do this would vary from ide to ide. If you are not using an IDE, make sure to pass all the files into the compiler so it can link them.
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

  3. #3
    Linux Guru jmadero's Avatar
    Join Date
    Jul 2007
    Location
    California
    Posts
    1,958
    How do I "build a project" using g++. Basically I have the header file and two cpp files in one folder, assumed that doing g++ main.cpp -o main would work but apparently I need to build a project. Main.cpp is dependent on a class defined in a header file and another cpp file defines the functions in the header file. Thanks in advance
    Bodhi 1.3 & Bodhi 1.4 using E17
    Dell Studio 17, Intel Graphics card, 4 gigs of RAM, E17

    "The beauty in life can only be found by moving past the materialism which defines human nature and into the higher realm of thought and knowledge"

  4. #4
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,301
    In standard c (gcc compiler) I usually compile them in order such as
    Code:
    gcc file1.c file2.c file3.c main.c -o myprog
    but with g++ it looks like you need to use the -combine flag.
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...