我似乎做错了什么,但我不确定是什么.这是我正在尝试做的最小的例子:
#include <iostream>
using std::cout;
class CallMe {
public:
void Maybe() {
cout << "A";
}
};
class TemplateValue {
public:
static CallMe call_me;
};
template<typename T>
void CallMemberMember() {
T::call_me.Maybe();
}
int main(int argc, char *argv[]) {
CallMemberMember<TemplateValue>();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试构建它时,我收到一个链接错误:
$ clang++ --std=c++11 repro_link_error.cc
Undefined symbols for architecture x86_64:
"TemplateValue::call_me", referenced from:
void CallMemberMember<TemplateValue>() in repro_link_error-9BE9gw.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
考虑功能:
void foo(int a = 3)//named default parameter
void foo1(int = 3)//unnamed default parameter
Run Code Online (Sandbox Code Playgroud)
我理解第一个函数的需要.("a"的值是3,它可以在程序中使用).但是第二个函数(这不是一个错误)已经初始化为3.如果我可以使用此值,我究竟如何使用此值...
我一直在寻找boost :: property_tree(用于读取json)的API,我可以用它来确定字段的值是树还是终值.例如,我有一个json,其中foo的值可以是第一个块中所示的树,也可以是第二个块中所示的字符串.
{
"foo": {
" n1": "v1",
"n2": "v2"
}
}
{
"foo": "bar"
}
Run Code Online (Sandbox Code Playgroud)
我知道我们可以先用get_child_optional查看.如果返回的值为null,那么我们可以检查get_optional.但有没有更好的方法/ apis这样做?
对于代码,为什么错误,osteam_iterator是一个模板类,为什么没有匹配的构造函数用于'ostream_iterator'的初始化,请给予帮助,谢谢.define ostream_iterator template> class _LIBCPP_VISIBLE ostream_iterator
int main(int argc, const char * argv[])
{
vector<int> sentence1;
sentence1.reserve(5);// ???????????
sentence1.push_back(1);
sentence1.push_back(2);
sentence1.push_back(3);
sentence1.push_back(4);
sentence1.push_back(5);
int c = 5;
copy(sentence1.begin(), sentence1.end(), ostream_iterator<int>(cout, 1));
cout << endl;
Run Code Online (Sandbox Code Playgroud) 我有一个函数,它接受一个结构的双指针并赋值.但是,当我尝试访问该成员时,我收到了"访问违规写入位置..." member1
.这是我的代码:
struct mystruct{
unsigned int member1;
void * data;
};
int main(){
mystruct **foo = new mystruct *;
bar(foo);
}
void bar(unsigned int val, mystruct ** foo)
{
(*foo)->member1 = val;
}
Run Code Online (Sandbox Code Playgroud) 所以我有多个方法使用不同的参数:
class c;
void c::foo1(int a) {
}
void c::foo2(int a, int b) {
}
Run Code Online (Sandbox Code Playgroud)
我如何拥有这些功能的队列/载体?它不一定必须是std :: function对象,但我需要一种方法来排队函数的执行.
根据这份文件:
http://www.stroustrup.com/terminology.pdf
关于这些,我有几个问题.
一个.具有身份的x值的例子是什么?以下是不合法的:
Foo f;
&std::move(f);
Run Code Online (Sandbox Code Playgroud)
湾 我可以重载类Foo的& -运算符,并使其返回这使以下成为合法的:
&Foo(5);
Run Code Online (Sandbox Code Playgroud)
但像Foo(5)这样的公关价值不具备同一性.还是对身份有更微妙的解释?
我正在尝试将一些类方法传递给某个函数并使用"函数调用缺少参数列表;使用'&'创建指向成员的指针"错误.
//There is some class
class A {
int someField;
void Add(int someAdd) {
someField += someAdd;
}
}
//And function
void someFunc(std::function<void(int x)> handler) {
//Some code
handler(234);
}
//Class method pass to function
void main() {
A* instanceA = new A();
someFunc(instanceA->Add); //Error 19 error C3867: 'A::Add': function call missing argument list; use '&A::Add' to create a pointer to member
}
Run Code Online (Sandbox Code Playgroud)
怎么了?
我有一个这样的类.我想使用字符串的引用,但它不起作用.我怎么能使用字符串&?
#include <string>
using namespace std;
class T
{
public:
T(string& s);
private:
string s;
};
T::T(string& s)
{
this->s = s;
}
int main(void)
{
T t("Test Object");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:'T :: T(std :: string&)':无法将参数1从'const char [12]'转换为'std :: string&'
例如:
class Airport : public vector<Airline*>
Run Code Online (Sandbox Code Playgroud)
如何正确释放内存?我不想使用智能指针,因为我在课程中还没有学到它。