输入以下代码后,我会收到错误.
const int quantity;
cout << "How much spacing do you want in-between the frames? " ;
cin >> quantity;
Run Code Online (Sandbox Code Playgroud)
错误:未初始化的const'数量'[ - fpermissive]
错误:'operator >>的模糊重载
如果我只使用int类型,就不会发生这种情况
int quantity;
cout << "How much spacing do you want in-between the frames? " ;
cin >> quantity;
Run Code Online (Sandbox Code Playgroud)
哪个编译没有问题.我是C++的新手,所以我想知道为什么会这样.
我有以下课程:
class DataBuilder
{
public:
DataBuilder(Data& data): data_(data){}
//Fun modify data_, uses private methods
void Fun(std::string name, int id) //const ?
{
//calculate newInfo based on params (uses private methods)
auto& info = data_.GetInfo();
info = newInfo;
}
private:
//some private methods
Data& data_;
};
Run Code Online (Sandbox Code Playgroud)
理论上有趣可以是const,但我想知道它是否正确(逻辑常量)?
Edit1我添加了一个非常简化的Fun实现. Edit2 Data有两个GetInfo重载:
Info& Data::GetInfo();
const Info& Data::GetInfo() const;
Run Code Online (Sandbox Code Playgroud) 我试图获得的存储类型被称为结构的对象在两个集合的交集dist
,使用std::set_intersection
.我希望结果存储在另一个中set<dist>
.
但是,编译器会出现以下错误:
In file included from /usr/include/c++/5/algorithm:62:0,
from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:64,
from /home/kirill/CLionProjects/contest/main.cpp:1:
/usr/include/c++/5/bits/stl_algo.h: In instantiation of ‘_OutputIterator std::__set_intersection(_InputIterator1, _InputIterator1, _InputIterator2, _InputIterator2, _OutputIterator, _Compare) [with _InputIterator1 = std::_Rb_tree_const_iterator<dist>; _InputIterator2 = std::_Rb_tree_const_iterator<dist>; _OutputIterator = std::_Rb_tree_const_iterator<dist>; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’:
/usr/include/c++/5/bits/stl_algo.h:5122:48: required from ‘_OIter std::set_intersection(_IIter1, _IIter1, _IIter2, _IIter2, _OIter) [with _IIter1 = std::_Rb_tree_const_iterator<dist>; _IIter2 = std::_Rb_tree_const_iterator<dist>; _OIter = std::_Rb_tree_const_iterator<dist>]’
/home/kirill/CLionProjects/contest/main.cpp:65:73: required from here
/usr/include/c++/5/bits/stl_algo.h:5076:16: error: passing ‘const dist’ as ‘this’ argument discards qualifiers [-fpermissive]
*__result = *__first1;
^
/home/kirill/CLionProjects/contest/main.cpp:26:8: …
Run Code Online (Sandbox Code Playgroud) #include "stdafx.h"
#include <iomanip>
#include <ostream>
#include <fstream>
using namespace std;
void FillArray(int x[ ], const int Size);
void PrintArray(int x[ ], const int Size);
int main()
{
const int SizeArray = 10;
int A[SizeArray] = {0};
FillArray (A, SizeArray);
PrintAray (A, SizeArray);
return 0;
}
void FillArray (int x[ ], const int Size)
{
for( int i = 0; i < Size; i++);
{
cout << endl << "enter an integer"; //cout undeclared here
cin >> x[i]; //cin and …
Run Code Online (Sandbox Code Playgroud) int main()
{
const int i = 10;
int *p = (int*) &i;
*p = 20;
printf("%d\n",i); // 10
printf("%d\n",*p); // 20
}
Run Code Online (Sandbox Code Playgroud)
我可以得到我的真实地址吗?如果我想改变i的价值,我该怎么办呢?原谅我可怜的英语!
我有这个:
// Setup the SQL Statement and compile it for faster access
NSString *sqlStatement = @"SELECT * FROM nameList";
const char *sqlStatementC = (const char* )sqlStatement;
Run Code Online (Sandbox Code Playgroud)
但ARC并不喜欢这样.有解决方法吗?
谢谢
我想给一个长度为x的数组.x是用户输入.但问题是x必须是常量.
这是我的代码:
int *length = 0;
cin >> (*length);
const int arraylength = const_cast<int>(*length);
int l[arraylength];
Run Code Online (Sandbox Code Playgroud) 我有一个理论问题;).在这本书中,我一直在学习C++(虽然它很棒,只有我的母语,所以标题不会指定任何东西)作者在#define
和const
变量之间进行了比较.他是第二种方法,因为它更适合调试,但他没有谈到的一件事就是内存管理.假设我们有很多常量我们希望定义.当然它可能需要很多内存.说实话,我还在学习,我从来没有需要这么多常数,但是当我学会了你可以选择之间short
,int
而且long
,我开始想,也许那些小位在大程序中有所作为.所以我的问题是:你怎么看?
在花了一些时间挖掘相关帖子/在线资源后,我仍然对我的问题感到困惑.我的示例代码(test.cc)是:
void testsub(const int* &xx );
int main ()
{
int* xx;
xx= new int [10];
testsub(xx);
}
void testsub(const int* & xx){}
Run Code Online (Sandbox Code Playgroud)
编译错误消息(pgcpp)读取
"test.cc", line 7: error: a reference of type "const int *&" (not const-qualified) cannot be initialized with a value of type "int *" testsub(xx); ^ 1 error detected in the compilation of "test.cc"."
为什么?非常感谢您的帮助.祝福,婷