gcc 4.8操作系统X 10.8上的编译错误 我教儿子C++,他想看看新的C++ 11个特性。我将GCC4.8编译为g++-4.8 $ gcc-4.8 gcc-4.8: fatal error: no input files compilation terminated.
运行简单示例失败,原因是:gcc 4.8操作系统X 10.8上的编译错误 我教儿子C++,他想看看新的C++ 11个特性。我将GCC4.8编译为g++-4.8 $ gcc-4.8 gcc-4.8: fatal error: no input files compilation terminated.,c++,gcc,c++11,compiler-errors,g++,C++,Gcc,C++11,Compiler Errors,G++,运行简单示例失败,原因是: $ 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
$ 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;
}
#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
#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;
}
但我仍然得到一个错误:
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;
}
多方面的原因,很多小的打字错误
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;
virtual ~Jason();
Jason::Jason j1 = Jason::Jason();
…已声明,但未实现
……还有
std::cout << "You also said hi by: " << s.c_str() << std::end;
virtual ~Jason();
Jason::Jason j1 = Jason::Jason();
虽然它显然是编译的,但可以简化为(谢谢chris)
这应该让你开始,我没有一个C++编译器来测试,所以可能不是全部:)/P>你正在使用一个。至于区别。您也可以直接打印
std::string
s,无需c_str
。您可以指出保留的标识符吗。另外,我有经常使用c_str()的习惯,我不知道为什么。但我总是这样。这是个坏习惯吗?原因?您还有一个ytpo:sd::string s
应该是std::string s
。您保留的标识符是\uu JASON\u H\uu
。而且c_str
也有它的用途,但它只是额外的代码,没有任何好处。除非你真的需要一个C字符串,为什么还要麻烦转换它呢?@Jason,是的,在我的第一个链接中详细描述了。关于最后一个部分,这两个都不是Jason j1
@chris True,但是前者应该编译并运行正常,除非我遗漏了什么。后者需要将用法更改为指针用法。无论是哪种方式,添加注释都是更好的方式。它应该可以工作,只是额外键入,需要一个可访问的副本构造函数,理论上,创建一个副本构造函数然后复制:)为什么是Jason:Jason j1=Jason::Jason()代码>错误?Jason是一个类,但如果我是:int I;杰森:某物(我);那就行了为什么行?我甚至在看一个例子,使这个电话在相同的方式。。。
Jason j1;