gcc 4.8在OS X 10.8上编译错误

时间:2013-06-01 22:36:45

标签: c++ gcc c++11 compiler-errors g++

我正在教我的儿子C ++,他想看看新的C ++ 11功能。我将gcc 4.8编译为g ++ - 4.8

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

运行一个简单示例失败:

$ 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)

我做了: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;
}

我退了一步,尝试使用系统默认的gcc:

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

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

但我仍然遇到一个错误:

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

任何人都可以帮助我理解为什么这会失败吗?

我尝试了一个简单的C ++ v11示例:

#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;
}

编译..

$ g++-4.8 -Wall main2.cpp  -o test -std=c++11
$ ./test
Hello, World!

1 个答案:

答案 0 :(得分:4)

有多个原因,那里有很多小错字;

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

应该是

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

...和...

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

......应该......

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

...和...

virtual ~Jason();

...已声明,但尚未实施

...和...

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

虽然它显然是编译,但可以简化为(谢谢克里斯)

Jason j1;

那应该让你开始,我没有一个C ++编译器来测试,所以可能不是全部:)