尝试编译以下C++代码片段时(完整源代码如下)
A::A(istream& i) {
vector<string> words( istream_iterator<int>(i), istream_iterator<int> );
words.begin();
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误
istream_it.cpp:12: error: request for member ‘begin’ in ‘words’, which is of non-class type
‘std::vector<int, std::allocator<int> >(
std::istream_iterator<int, char, std::char_traits<char>, long int>,
std::istream_iterator<int, char, std::char_traits<char>, long int>)’
Run Code Online (Sandbox Code Playgroud)
我知道这个错误通常是由使用no-parameters运算符意外声明一个函数引起的,如
string s(); s.size();
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我已经删除了所有不必要的代码,仍然无法确定究竟出现了什么问题,或者正确的语法是什么.
完整来源:
#include <sstream>
#include <vector>
#include <iterator>
#include <string>
using namespace std;
class A {
public:
A(istream& i) {
vector<int> words(istream_iterator<int>(i), istream_iterator<int> );
words.begin();
}
};
int main(int argc, char** argv)
{
istringstream iss("1 2 3");
A …Run Code Online (Sandbox Code Playgroud) 我在matlab中有两个函数,大致如下所示
function f1()
setup_callback(@f2);
a = 1;
evaluate_callback();
end
function f2()
...
end
Run Code Online (Sandbox Code Playgroud)
其中evaluate_callback是一个调用f2的外部库函数.
我希望能够从f2中读取a的当前值.有没有办法在不使用全局变量的情况下实现这一目标?
我不明白这个片段的行为:(用clang ++ 3.0编译)
#include <iostream>
using namespace std;
class Base {
public:
virtual void bar() {}
bool foo = false;
};
class Derived : public Base{
public:
Derived() { Base::foo = true; }
};
int main() {
Derived d;
Base b(d);
cout << b.foo << endl; // prints 0
// prints 1 if "virtual void bar() {}" is removed
}
Run Code Online (Sandbox Code Playgroud)
为什么Base :: bar()函数对Base :: foo的复制有影响?
与static_cast相比,即.所以,如果我们有这两个演员阵容
Base* b(new Derived());
Derived* d = static_cast<Derived*>(b); // (1)
shared_ptr<Base> b(new Derived());
shared_ptr<Derived> d = static_pointer_cast<Derived>(b); // (2)
Run Code Online (Sandbox Code Playgroud)
第(2)行会比第(1)行慢吗?
说我有这个程序
#include <iostream>
using namespace std;
template<typename T>
class A {
public:
void foo() { cout << "Inside foo" << endl; }
void bar() { cout << "Inside bar" << endl; }
};
int main() {
A<int> a;
a.foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++和clang ++都只生成代码A<int>::foo(),但不是为A<int>::bar().当你想在调试时调用这个函数时,这很烦人.(例如vector<T>::at()).
是否有一些标志或其他方法可以在每次实例化模板时强制为所有成员函数生成代码?
我正在搜索一个哈希函数,它将任何整数作为输入(正或负,但如果这使得它更容易被约束到int范围),并返回介于-1和1之间的实数.是否有这样的函数,或从另一个哈希函数构建它的任何明显方法?
该功能不必是安全的,只要其足够"随机"即可.如果存在C/C++实现,则奖励积分.
如果在文件名错误的文件中定义了公共类,javac则会抛出错误:
square_supplies.java:5: error: class Answer is public, should be declared in a file named Answer.java
Run Code Online (Sandbox Code Playgroud)
是否有一个标志可以关闭此行为?如果它不可能javac,是否有另一个java编译器可以关闭它?
我正在尝试编写一个 rust (meta-) 函数,将一些输入类型映射到一些不相关的输出类型。
来自 C++ 背景,我通常会这样写:
template<typename T>
struct f;
template<>
struct f<int> { using type = double; };
using input_type = int;
using output_type = f<input_type>::type;
Run Code Online (Sandbox Code Playgroud)
我天真地尝试在 Rust 中编写相同的内容如下:
macro_rules! f {
($in: ty) => {
match $in {
i32 => f32,
}
}
}
type OutputType = f!(i32);
Run Code Online (Sandbox Code Playgroud)
但是,好吧,这无法编译,因为宏显然没有返回类型。
$ rustc typedef.rs
error: expected type, found keyword `match`
--> typedef.rs:3:5
|
3 | match $in {
| ^^^^^ expected type
...
9 | type OutputType = f!(i32); …Run Code Online (Sandbox Code Playgroud) 在下面的例子中
void fun() {
if(int i=SOME_VALUE) {
// ...
} else {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
i 的范围是什么?我们可以在 if 块中使用它吗?我们可以在 else 块中使用它吗?
编辑:
作为后续,在这个修改后的示例中
void fun() {
if(int i=SOME_VALUE) {
// ...
} else if(int j=SOME_OTHER_VALUE){
// ...
} else {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以在 else 子句中同时访问 i 和 j 吗?