-1

I am reading this book called "C++ How to Program" from Deitel and I am still a beginner in this. I know Java, so I am trying to get familiar with the C++ syntax and how it works.

My code is the following:

file >> gradebook_interface.h

#include <string>
//#include <unordered_map>
using namespace std;

//specifing interface

class gradebook_interface
{
    public:
        //constructor
        gradebook_interface(string);
        void reset_Coursename();
        void setCoursename(string);
        string getCourseName();
        void displayMessage();
        void add_to_hashmap(string,int);

    private:
        //hashmap init
        //unordered_map <string, int> course_map;
        string courseName;
};

file >> gradebook_interface.cpp

#include <iostream>
//including interface "gradeinterface"
#include "gradebook_interface.h"

using namespace std;

    //constructor from interface
    gradebook_interface::gradebook_interface(string name)
    {
        setCoursename(name);
    }

    void gradebook_interface::setCoursename ( string name )
    {
        courseName = name;
    }

    string gradebook_interface::getCourseName()
    {
        return courseName;
    }

    void gradebook_interface::displayMessage()
    {
        cout << "First C++ application\n" << getCourseName() << "!" << endl;
    }

    //interfac's methods
    void gradebook_interface::reset_Coursename()
    {   
        courseName = "null";
        cout << "The course name has been reseted! Value is: " << getCourseName() << endl;
    }

    void gradebook_interface::add_to_hashmap(string, int)
    {
        //course_map["test_course"] = 14;
        //cout << "Hashmap value just entered:" << endl;
        //cout << course_map["test_course"] << endl;

    } 

file >> gradebook_main.cpp

#include <iostream>
#include "gradebook_interface.h"

using namespace std;

int main()
{
    gradebook_interface gradebook_1 ("Maths");
    gradebook_interface gradebook_2 ("Greek");

    cout << "gradebook 1 " << gradebook_1.getCourseName() << endl;
    cout << "gradebook 2 " << gradebook_2.getCourseName()  << endl;

};

The book uses Visual Studio that I can't use because I am on ubuntu. I read somewhere that if you compile C++ code using "gcc" it invokes automatically "g++". But I get an error, so I am forced to use g++. My first question is, can I use gcc (any parameters needed?)? My second question is, how does the linker works? My third question is why do I get a segmentation fault when I try to run this ??

thanks

JoeG
  • 12,994
  • 1
  • 38
  • 63
Trt Trt
  • 5,330
  • 13
  • 53
  • 86
  • 5
    1) gcc is for c code, g++ is for c++ code. 2) The compiler turns source code into object code, and the linker binds object code into an executible. 3) Please post your seg fault. – Nate Oct 11 '11 at 19:41
  • 4
    Looks like you're suffering from multiple, unrelated problems. Maybe best to sort them out one by one. Do you actually get a segfault? The code looks OK. Make sure you're compiling the right thing... – Kerrek SB Oct 11 '11 at 19:42
  • What errors do you get, and what does the stack trace look like when you get the segfault? The code looks alright from a cursory glance. – Chad Oct 11 '11 at 19:43
  • Is it me, or do the actual questions have nothing at all to do with the title? – cHao Oct 11 '11 at 19:43
  • More of a side comment, but add some multiple inclusions guards on your H file :) #ifndef/#define/#endif, and I don't see how it could seg fault, can we see your error? – John Humphreys Oct 11 '11 at 19:44
  • How did you compile the program? Post your g++ invocation – Tio Pepe Oct 11 '11 at 19:51
  • @cHao: I guess there is an assignment due soon, and on that prospect sombody "dumped core". – Benjamin Bannier Oct 11 '11 at 19:53
  • Another side note: Prefer fully qualified names `std::string`, `std::cout` over `using namespace std;` – AJG85 Oct 11 '11 at 19:56
  • @cHao: I was rather hoping for the sequel "c++ advanced segmentation faults" and "c++ expert segmentation faults" :) – sehe Oct 11 '11 at 20:04
  • The code actually runs fine in ideone (which uses GCC), BTW. – cHao Oct 11 '11 at 20:10
  • @TioPepe I compiled all the files. Each one separately – Trt Trt Oct 11 '11 at 20:11
  • @TrtTrt : what commands did you issue in order to compile the files? – Robᵩ Oct 11 '11 at 20:21
  • g++ file1, g++ file2,g++ file3 (just simple g++ command) – Trt Trt Oct 11 '11 at 20:24
  • @Trt Trt: There are a number of IDEs (Integrated Development Environment) that are similar to Visual Studio and are freely available in the Ubuntu repositories. You may prefer using an IDE to working from the command line. See http://stackoverflow.com/questions/24109/c-ide-for-linux. I personally prefer Code::Blocks. – Emile Cormier Oct 11 '11 at 20:40

1 Answers1

2

I have no problem compiling your program in this way:

g++ -O2 -g main.cpp gradebook_interface.cpp -o main

In fact I used the following Makefile

all: main

main: main.cpp gradebook_interface.cpp
    g++ -O2 -g $^ -o $@

and simple issued make && ./main outputting:

gradebook 1 Maths
gradebook 2 Greek

I ran it under valgrind; so far no detected memory errors. I haven't looked at your code

Edit On the How does the linker work part:

If you rewrote that Makefile like so:

all: main

CPPFLAGS+=-O2 -g

main: main.o gradebook_interface.o
    g++ $(CPPFLAGS) $^ -o $@

It would result in the following build steps:

g++  -O2 -g  -c -o main.o main.cpp
g++  -O2 -g  -c -o gradebook_interface.o gradebook_interface.cpp
g++ -O2 -g main.o gradebook_interface.o -o main

which shows how to do the compile steps (-c) separately from the link step. The link step could in that case be written as the explicit call to ld intead, but you'll have to specify the runtime libraries for libgcc and libstdc++ yourself

sehe
  • 374,641
  • 47
  • 450
  • 633