我尝试这样做:调用一个函数,该函数接受引用作为参数,而不传递“变量”,只是传递值。难道我的编译器(Gcc)不能创建临时的“变量”来传入吗?似乎mvc是以某种方式(项目中的其他人使用它)来做这件事的。
我有:
foo(Vector&,Vector&)
每当我尝试调用foo(Vector(1,2,3),Vector(4,5,6))时,都会收到no matching function for call to foo(Vector,Vector); note: candidates are foo(Vector&,Vector&)
我该怎么办?为什么不动呢?有什么我不理
// have compilation errors
class TestClass
{
public:
TestClass(std::string& str) {}
};
int main() {
TestClass tc(std::string("h"));
return 0;
}
p166.cpp: In function ‘int main()’:
p166.cpp:29:32: error: no matching function for call to ‘TestClass::TestClass(std::string)’
p166.c
可能重复:
有这样的守则:
void fun_ref(int& par){}
void fun_const_ref(const int& par){}
int main(){
//fun_ref(2); error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’
fun_const_ref(2);
char var = 3;
//fun_ref(var); error: invalid initializ
考虑以下处理const引用的代码:
const int & func (const int &x)
{
return x;
}
struct Foo {
Foo (const int &x)
: m_x(x) {}
const int & getX ()
{ return m_x; }
const int &m_x;
};
我想知道现在是否允许下列哪一项:
int x = func(int(7));
int y = Foo(int(7)).getX();
是否可以保证临时int对象在被赋值或getX使
为什么这段代码无法编译?
double d ;
int & i1 = d // Compilation FAILS
而这一次呢?
double d ;
const int & i = d // Compilation Succeeds
请告诉我,我很想知道C++设计者的想法是什么,他们允许一种行为而不允许另一种。
我知道无论在哪种情况下,这都是不道德的,与技术上是否可行无关。另外仅供参考,我在mac上使用的是带有"-O0 -g3 -Wall -c -fmessage-length=0“的GCC
#include <iostream>
void f(const int * & p)
{
int i =0;
i = p[0];
std::cout << i << std::endl;
}
int main()
{
int * p =new int[1];
p[0] =102;
f(p);
return 1;
}
对于此代码,gcc编译器会给出错误:
prog.cpp: In function ‘int main()’:
prog.cpp:16: error: invalid init
我只是在名称空间中创建了一个很酷的类,因此我想创建2个函数。他们应该有相同的名字,但不同的参数。
但是每次我编译它的时候,它都说:
.....formatter.cpp:25:18: error: no matching member function for call to 'output'
return this->output(DateTime());
~~~~~~^~~~~~
.....formatter.h:57:18: note: candidate function not viable: expects an l-value for 1st
我们都知道这样的事情在c++中是有效的:
const T &x = T();
同时:
T &x = T();
不是。
在中,对话导致了这条规则。OP发布了一些代码,这些代码清楚地让人联想到UB。但我希望修改后的版本能够工作(这是修改后的版本):
#include <iostream>
using namespace std;
class A {
public:
A(int k) { _k = k; };
int get() const { return _k; };
int _k;
};
class B {
public:
B(c
错误!
$ cat double.cc
#include<iostream>
int main() {
int i = 42;
double &r = i;
}
$ g++ double.cc
double.cc: In function ‘int main()’:
double.cc:5: error: invalid initialization of reference of type ‘double&’
from expression of type ‘int’
$
成功了!
$ cat double.cc
#include<iost
void func(char**& arg1);
int main() {
char* container[3] = { "First", "Second", "Third" };
char** pCon = &container[0];
func(pCon); // This works
func(&container[0]); // no known conversion from char** to char**&
}
很明显我在这里漏掉了什么。我的逻辑是这两者应
今天早上我正在编写一些模板代码,其中我使用了一个BOOST_STATIC_ASSERT来确保我没有创建对错误类型的引用,因为我认为这可能是一个更清晰的错误消息。但是,当我尝试删除静态断言以查看替代编译器错误时,我震惊地发现,当您尝试创建const double时,gcc甚至没有抱怨&引用的是int:
#include <iostream>
int main()
{
int x = 5;
const double& y = x;
std::cout << y << std::endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
vector<int> func()
{
vector<int> a(3,100);
return a;
}
int main()
{
vector<int> b(2,300);
//b.swap(func()); /* why is this not working? */
func().swap(b); /* and why is this working? */
return 0;
}
在上
我不明白为什么下面的代码不能工作:
#include <iostream>
using namespace std;
class PayOff{};
class PayOffCall : public PayOff{};
class PayOffBridge{
public:
PayOffBridge( PayOff& innerPayOff){};
};
class VanilaOption{
public:
VanilaOption(PayOffBridge& ThePayOff_){cout << " test "
我重载了下面的大于运算符:
bool operator > (Person & a, Person & b)
{
//firstname is a string data type
return (a.FirstName > b.FirstName);
}
如果我有类似下面这样的代码,它就能正常工作:
Person a = myPersonA;
Person b = myPersonB;
return myPersonA > myPersonB;
但是,在我的Person类中,我定义了一个Person getByID(int id)函
考虑以下代码:
struct foo
{
foo(foo const&) = default; // To make sure it exists
};
foo& get_local_foo_reference()
{
foo my_local_foo;
return my_local_foo; // Return a reference to a local variable
}
int main()
{
foo my_foo = get_local_foo_reference();
}
现在,每个人都会同意,返回对局部变量的引用是不好
我有给定的代码,这将得到一个错误:
错误:从类型' int‘const b=f(a++)的r值初始化'int&’类型的非const引用无效;^
int f(int& a)
{
return a;
}
int main() {
// your code goes here
int a = 5;
int b = f(a++);
std::cout << b << std::endl;
return 0;
}
造成这个错误的原因是什么?
请允许我先发布我的代码:
void Fun(short &s){}
void FunCon(const short &s){}
int main()
{
int iTest = 20;//note: iTest is int but parameter of Fun is "short &"
Fun(iTest);//error, I know there is a temp variable(typecast)
FunCon(iTest);//ok
return 0;
}
我知道Fun(iTest);会生成一个新的临时变量(Typec