const这些声明中的含义是什么?该const混淆了我.
class foobar
{
public:
operator int () const;
const char* foo() const;
};
Run Code Online (Sandbox Code Playgroud) 考虑下面的简单程序,它尝试使用对其中元素的NON-const引用来遍历集合的值:
#include <set>
#include <iostream>
class Int
{
public:
Int(int value) : value_(value) {}
int value() const { return value_; }
bool operator<(const Int& other) const { return value_ < other.value(); }
private:
int value_;
};
int
main(int argc, char** argv) {
std::set<Int> ints;
ints.insert(10);
for (Int& i : ints) {
std::cout << i.value() << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时,我从gcc收到错误:
test.c: In function ‘int main(int, char**)’:
test.c:18:18: error: invalid initialization of reference of type ‘Int&’ from expression of type …Run Code Online (Sandbox Code Playgroud) 错误:将'const A'作为'void A :: hi()'的'this'参数传递,丢弃限定符[-fpermissive]
我不明白为什么我得到这个错误,我没有返回任何东西只是传递对象的引用,就是这样.
#include <iostream>
class A
{
public:
void hi()
{
std::cout << "hi." << std::endl;
}
};
class B
{
public:
void receive(const A& a) {
a.hi();
}
};
class C
{
public:
void receive(const A& a) {
B b;
b.receive(a);
}
};
int main(int argc, char ** argv)
{
A a;
C c;
c.receive(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
@编辑
我使用const正确性修复它但现在我试图在同一个方法中调用方法并得到相同的错误,但奇怪的是我没有传递对此方法的引用.
#include <iostream>
class A
{
public:
void sayhi() const
{
hello();
world();
}
void hello() …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个struct. 其中一个不会更改,因此被声明const,但另一个可能会异步更改,因此我想制作它volatile。
我正在尝试使用const结构的实例来初始化volatile一个。但是,如果我使用volatile关键字编译器会抛出这个错误:
passing 'volatile rect' as 'this' argument of 'rect& rect::operator=(rect&&)' discards qualifiers [-fpermissive]at line 15 col 8
Run Code Online (Sandbox Code Playgroud)
#include <Arduino.h>
struct rect {
int x0;
int y0;
int width;
int height;
};
const rect outer = {0, 0, 10, 5};
volatile rect inner;
void setup() {
inner = {outer.x0 + 1, outer.y0 + 1,
outer.width - 2, outer.height - 2};
}
void loop() { …Run Code Online (Sandbox Code Playgroud) 所以,我正在尝试创建一个结构TileSet并覆盖<运算符,然后将其放入TileSet优先级队列中.我已经读过我不能在const引用上调用非const方法,但实际上不应该有问题,我只是访问成员,而不是更改它们:
struct TileSet
{
// ... other struct stuff, the only stuff that matters
TileSet(const TileSet& copy)
{
this->gid = copy.gid;
this->spacing = copy.spacing;
this->width = copy.width;
this->height = copy.height;
this->texture = copy.texture;
}
bool operator<(const TileSet &b)
{
return this->gid < b.gid;
}
};
Run Code Online (Sandbox Code Playgroud)
错误消息告诉我:传递'const TileSet' as 'this' argument of 'bool TileSet::operator<(const TileSet&)' discards qualifiers [-fpermissive]这是什么意思?将变量更改为const不起作用,无论如何我都需要它们是非const的.
我尝试这样做时会发生错误:
std::priority_queue<be::Object::TileSet> tileset_queue;
我只是想知道,为什么没有人解决我最近遇到的与 google protobufs 相关的问题,但是经过广泛的谷歌搜索、阅读谷歌手册页的文档并在 Stackoverflow-DB 中搜索后,我没有找到解决方案。
我在 Ubuntu 14.04.3 LTS 上使用 proto2-c++-API,通过 cmake 文件使用 gcc/g++ 进行编译。
我有一个从文件中读取二进制(序列化)google 协议缓冲区消息的应用程序。该程序的目的是将消息(不进行反序列化)发送到另一个应用程序,该应用程序继续处理实际数据。
我现在想修改一些消息,从文件中读取,以便我可以测试第二个应用程序的功能。不幸的是,我的消息包含很多嵌套消息,因此在反序列化之后我必须调用类似
message().a().b().c()....x().value();
Run Code Online (Sandbox Code Playgroud)
能够处理实际数据。
我现在的问题是,如何在x不创建另一个类型的消息的情况下更改 的值,message其中我还必须创建所有子消息 ( a,b,c...) 并将这些分配给各自的前任,就像下面的伪代码一样?!
a = new a();
b = new b();
c = new c();
...
v = new v();
w = new w();
x = new x();
x.set_value();
w.set_allocated_x_value(x);
v.set_allocated_w_value(w);
...
a.set_allocated_b_value(b);
message.set_allocated_a_value(a);
...
/* forward message to second application */
...
delete x;
delete w;
...
delete a;
Run Code Online (Sandbox Code Playgroud)
显然,不可能set_value …
我正在使用 C++11 来做一些线程程序。
现在我遇到这样的情况:
我有一个std::set<std::future<std::string>> results存储线程的一些结果,当然所有这些线程都会返回一个字符串。
但是,当我尝试获取字符串时,出现以下错误:
将 xxx 作为 xxx 的“this”参数传递会丢弃限定符
根据此链接,我认为这是因为我试图调用属于 元素的非常量函数set。换句话说, the 的元素set是std::future<std::string>并且std::future<std::string>::get()是非常量。这就是为什么我收到这样的错误。
如果我是对的,这是否意味着我永远不能声明 astd::set<std::future>因为它get总是不可用?
这是我的代码:
set<future<string>> results;
results.insert(...); // insert some future
for(auto it = results.begin(); it != results.end();)
{
if (it->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
string tmp = it->get(); // ERROR!!!
}
}
Run Code Online (Sandbox Code Playgroud)