我上课了
class foo {
public:
foo();
foo( int );
private:
static const string s;
};
Run Code Online (Sandbox Code Playgroud)
在s源文件中初始化字符串的最佳位置在哪里?
为什么这不起作用
for( int i = 0, int x = 0; some condition; ++i, ++x )
Run Code Online (Sandbox Code Playgroud)
这是
int i, x;
for( i = 0, x = 0; some condition; ++i, ++x )
Run Code Online (Sandbox Code Playgroud)
谢谢
在C++中,如果我想要一个指针数组,以便我可以让它们在后一阶段指向不同的对象,那么语法是什么样的.
编辑
我想澄清一下我想做什么.我有一个类foo,它有和添加方法.在add方法中,我引用了类bar.我想将该引用保存到bar的指针数组中.指针栏数组需要一直扩展,这样我就没有问题了.它是在指针堆上创建和数组,以便我稍后可以为它们分配bar对象.我试过的似乎失败了,因为类bar没有编译器抱怨的默认构造函数.这让我想到我正在创建一些我不想做的实际对象.
请不要,我不想听到你认为这是多么疯狂等等,这是你的意见.
我有类foo的以下成员.
foo &foo::bar()
{
return this;
}
Run Code Online (Sandbox Code Playgroud)
但我收到编译器错误.我做错了什么蠢事?
编译器错误(gcc):错误:从类型为'foo*const'的临时类中初始化'foo&'类型的非const引用
我有一个头文件,有一些前向声明但是当我在实现文件中包含头文件时,它包含在先前的前向声明的包含之后,这会导致像这样的错误.
error: using typedef-name ‘std::ifstream’ after ‘class’
/usr/include/c++/4.2.1/iosfwd:145: error: ‘std::ifstream’ has a previous declaration.
class ifstream;
class A
{
ifstream *inStream;
}
// End of A.h
#include <ifstream>
using std::ifstream;
#include "A.h"
// etc
Run Code Online (Sandbox Code Playgroud)
什么是解决这个问题的常态?
提前致谢.
是否可以在ios :: in和ios :: out不存在的文件上打开fstream而不会出现错误?
我想实现最大的整数函数.["最大整数函数"是一个非常 标准的名称,也称为地板函数.]
int x = 5/3;
Run Code Online (Sandbox Code Playgroud)
我的问题是,由于5/3会产生双倍的精度,可能会有更多的精度损失?
编辑:最大整数函数是小于或等于X的整数.示例:
4.5 = 4
4 = 4
3.2 = 3
3 = 3
Run Code Online (Sandbox Code Playgroud)
我想知道的是5/3会产生双倍?因为如果是这样,我将在转换为int时失去精度.
希望这是有道理的.
是否有人能够解释为什么头文件有这样的东西?
class foo; // This here?
class bar
{
bar();
};
Run Code Online (Sandbox Code Playgroud)
使用它时是否需要包含声明?
谢谢.
我有以下声明.
namespace test{
static cl_option* find_opt(const int &val, const cl_option *options);
}
test::cl_option* test::find_opt(const int &val, cl_option *options){}
Run Code Online (Sandbox Code Playgroud)
问题是编译时我得到以下错误.
error: ‘test::cl_option* test::find_opt(const int&, test::cl_option*)’ should have been declared inside ‘test’
Run Code Online (Sandbox Code Playgroud)
提前致谢
我用gcc收到以下错误.
invalid conversion from ‘char**’ to ‘const char**’
Run Code Online (Sandbox Code Playgroud)
有了这段代码.
void foo( const int &argc, const char **argv );
int main( int argc, char *argv[] )
{
foo( argc, argv );
}
Run Code Online (Sandbox Code Playgroud)
为什么是这样?