-1

I am teaching my son C++ and he wanted to look at new C++ 11 features. I compiled gcc 4.8 as g++-4.8

$ gcc-4.8
gcc-4.8: fatal error: no input files
compilation terminated.

Running a simple example fails with:

$ g++-4.8 -Wall main.cpp Jason.h Jason.cpp -o jason
main.cpp: In function ‘int main()’:
main.cpp:15:2: error: ‘Jason::Jason’ names the constructor, not the type
  Jason::Jason j1 = new Jason::Jason();
  ^
main.cpp:15:15: error: expected ‘;’ before ‘j1’
  Jason::Jason j1 = new Jason::Jason();
           ^
main.cpp:15:38: error: statement cannot resolve address of overloaded function
  Jason::Jason j1 = new Jason::Jason();
                                  ^
main.cpp:17:2: error: ‘j1’ was not declared in this scope
  j1.sayHi("Howdy");
  ^
Jason.cpp:12:19: error: expected initializer before ‘sayHi’
 void Jason::Jason sayHi(sd::string s)

I did: g++-4.8 -Wall main.cpp Jason.h Jason.cpp -o jason

main.cpp:

#include "Jason.h"

#include <iostream>
#include <string>

int main()
{
    std::cout << "Hi" << std::endl;

    std::string s = "testing";

    std::cout << "s: " << s.c_str() << std::endl;

    Jason::Jason j1 = Jason::Jason();

    j1.sayHi("Howdy");

    return 0;
}

Jason.h:

#ifndef __JASON_H__
#define __JASON_H__

#include <iostream>
#include <string>

class Jason
{
    public:
        Jason();
    virtual ~Jason();

        void sayHi(std::string s);

    protected:
        std::string hi; 

};
#endif

Jason.cpp:

#include "Jason.h"

Jason::Jason()
{
    hi = "Hello";

    std::cout << "You said Hi like: " << hi.c_str() << std::endl;   
}

void Jason::Jason sayHi(sd::string s)
{
    std::cout << "You also said hi by: " << s.c_str() << std::end;
}

I took a step back and tried with the system default gcc:

$ g++
i686-apple-darwin11-llvm-g++-4.2: no input files

$ g++ -Wall main.cpp Jason.h Jason.cpp -o jason

But I still get a single error:

Jason.cpp:12: error: expected initializer before ‘sayHi’

Can anyone help me understand why this is failing?

I tried a simple C++v11 example:

#include <iostream>
#include <thread>

//This function will be called from a thread
void call_from_thread() {
   std::cout << "Hello, World!" << std::endl;
}

int main() {
   //Launch a thread
   std::thread t1(call_from_thread);

    //Join the thread with the main thread
    t1.join();

    return 0;
}

Compiling..

$ g++-4.8 -Wall main2.cpp  -o test -std=c++11
$ ./test
Hello, World!
vidit
  • 6,293
  • 3
  • 32
  • 50
Jasmine
  • 15,375
  • 10
  • 30
  • 48
  • You're using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). As for the difference, http://stackoverflow.com/questions/12135498/why-are-redundant-scope-qualifications-supported-by-the-compiler-and-is-it-lega. You can also print `std::string`s directly, no need for `c_str`. – chris Jun 01 '13 at 22:40
  • can you point out the reserved identifier. Also, I have this habit of using c_str() a lot and I am not sure why. But I always do. Is that a bad habit? reason? – Jasmine Jun 01 '13 at 22:44
  • You also have a ytpo in there: `sd::string s` should be `std::string s`. – Paul R Jun 01 '13 at 22:45
  • 1
    Your reserved identifier is `__JASON_H__`. And `c_str` has its uses, but it's just extra code for no benefit here. Unless you really *need* a C string, why bother converting it? – chris Jun 01 '13 at 22:46
  • @Jason, Yes, the details are outlined pretty clearly in my first link. – chris Jun 01 '13 at 22:49
  • @chris, oh geez, that is a link. Thanks. Sorry about that. – Jasmine Jun 01 '13 at 22:50
  • I had no idea no "E" allowed – Jasmine Jun 01 '13 at 22:56
  • Do not compile header (*.h) files. Only give the *.cpp files to GCC. The header files are then included by the *.cpp files that need them. – Nikos C. Jun 01 '13 at 23:15

1 Answers1

4

Multiple reasons, lots of minor typos in there;

void Jason::Jason sayHi(sd::string s)

should be

void Jason::sayHi(std::string s)

...and...

std::cout << "You also said hi by: " << s.c_str() << std::end;

...should be...

std::cout << "You also said hi by: " << s.c_str() << std::endl;

...and...

virtual ~Jason();

...is declared, but not implemented

...and...

Jason::Jason j1 = Jason::Jason();

while it apparently compiles, can be simplified to (thanks chris)

Jason j1;

That should get you started, I don't have a C++ compiler to test with so may not be all :)

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • About the last part, neither of those are preferable, especially not the second, to `Jason j1;`. – chris Jun 01 '13 at 22:48
  • @chris True, but the former should compile and run ok unless I'm missing something. The latter would require changing the usage to pointer usage. Adding your comment either way, it is indeed the better way. – Joachim Isaksson Jun 01 '13 at 22:51
  • It should work, it's just extra typing, requires an accessible copy constructor, and theoretically, creates one and then copies it :) – chris Jun 01 '13 at 22:56
  • Why is `Jason:Jason j1 = Jason::Jason();` wrong? Jason is a class, but if I did: int i; Jason::something(i); that would work – Jasmine Jun 01 '13 at 22:59
  • so why does it work? I am even looking at an example that makes this call in the same way... – Jasmine Jun 01 '13 at 23:04
  • @Jason Finding a compiler, it seems I was wrong on the namespace part, Jason::Jason indeed compiles with clang++. Could have sworn and all that... :) Will remove that part from the answer. – Joachim Isaksson Jun 01 '13 at 23:23