标签: const

为什么我不能从用户读取值并使其成为常量?

输入以下代码后,我会收到错误.

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++的新手,所以我想知道为什么会这样.

c++ iostream const

-1
推荐指数
1
解决办法
109
查看次数

const正确性和成员参考

我有以下课程:

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)

c++ const

-1
推荐指数
1
解决办法
63
查看次数

std :: set_intersection的struct对象

我试图获得的存储类型被称为结构的对象在两个集合的交集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)

c++ containers stl const set

-1
推荐指数
1
解决办法
137
查看次数

在C++中使用main之后添加其他函数时未声明的标识符?

#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)

c++ arrays const function

-2
推荐指数
2
解决办法
2308
查看次数

如何从"const int i"获取i的真实地址

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的价值,我该怎么办呢?原谅我可怜的英语!

c++ const

-2
推荐指数
1
解决办法
1321
查看次数

iOS:如何在启用ARC的情况下转换为const char?

我有这个:

// 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并不喜欢这样.有解决方法吗?

谢谢

casting const char ios

-2
推荐指数
1
解决办法
799
查看次数

c ++ const int输入

我想给一个长度为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++ int const input

-2
推荐指数
1
解决办法
3736
查看次数

在Java中,何时使用const而不是final?

看来这两个是相同的.对于只读变量,是否可以提高性能?

const ant;
final ant;
Run Code Online (Sandbox Code Playgroud)

java final const

-2
推荐指数
1
解决办法
228
查看次数

#define与C++中的const

我有一个理论问题;).在这本书中,我一直在学习C++(虽然它很棒,只有我的母语,所以标题不会指定任何东西)作者在#defineconst变量之间进行了比较.他是第二种方法,因为它更适合调试,但他没有谈到的一件事就是内存管理.假设我们有很多常量我们希望定义.当然它可能需要很多内存.说实话,我还在学习,我从来没有需要这么多常数,但是当我学会了你可以选择之间short,int而且long,我开始想,也许那些小位在大程序中有所作为.所以我的问题是:你怎么看?

c++ const c-preprocessor

-2
推荐指数
1
解决办法
385
查看次数

函数中的c ++ - "引用...的错误无法用值初始化"

在花了一些时间挖掘相关帖子/在线资源后,我仍然对我的问题感到困惑.我的示例代码(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"."

为什么?非常感谢您的帮助.祝福,婷

c++ const reference

-2
推荐指数
1
解决办法
523
查看次数

标签 统计

const ×10

c++ ×8

arrays ×1

c-preprocessor ×1

casting ×1

char ×1

containers ×1

final ×1

function ×1

input ×1

int ×1

ios ×1

iostream ×1

java ×1

reference ×1

set ×1

stl ×1