2013-06-01 40 views
-1

我在教我的兒子C++,他想看看新的C++ 11功能。我已編譯的gcc 4.8爲g ++ - 4.8gcc 4.8在OS X上編譯錯誤10.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! 
+0

你正在使用[保留標識符](HTTP://計算器。COM /問題/ 228783 /什麼,是最規則有關,使用安下劃線-IN-A-C-標識符)。至於差異,http://stackoverflow.com/questions/12135498/why-are-redundant-scope-qualifications-supported-by-the-compiler-and-is-it-lega。你也可以直接打印'std :: string's,不需要'c_str'。 – chris

+0

你能指出保留的標識符嗎?此外,我有這種習慣使用c_str()很多,我不知道爲什麼。但我總是這樣做。這是一個壞習慣嗎?原因? – Jason

+0

你還有一個ytpo:'sd :: string s'應該是'std :: string s'。 –

回答

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++編譯器來測試,所以可能不是所有:)

+0

關於最後一部分,對傑森j1而言,這兩者都不可取,尤其不是第二位。 – chris

+0

@chris真的,但前者應該編譯並運行正常,除非我錯過了一些東西。後者需要將用法更改爲指針使用。無論哪種方式添加您的評論,這確實是更好的方法。 –

+0

它應該工作,它只是額外的輸入,需要一個可訪問的拷貝構造函數,理論上,創建一個,然後複製它:) – chris