小编scc*_*ccs的帖子

静态功能:此处可能未指定存储类

我已经以这种方式在我的类中定义了一个函数static(相关代码片段)

#ifndef connectivityClass_H
#define connectivityClass_H

class neighborAtt
{
public:
    neighborAtt(); //default constructor
    neighborAtt(int, int, int);

    ~neighborAtt(); //destructor

    static std::string intToStr(int number);

private:
    int neighborID;
    int attribute1;
    int attribute2;

#endif
Run Code Online (Sandbox Code Playgroud)

并在.cpp文件中

#include "stdafx.h"
#include "connectivityClass.h"

static std::string neighborAtt::intToStr(int number)
{
    std::stringstream ss; //create a stringstream
   ss << number; //add number to the stream
   return ss.str(); //return a string with the contents of the stream
}
Run Code Online (Sandbox Code Playgroud)

我在.cpp文件中收到错误(VS C++ 2010),说"这里可能没有指定存储类",我无法弄清楚我做错了什么.

ps我已经读过看起来像重复但我不知道 - 就像他一样 - 我是对的,编译器很挑剔.任何帮助表示赞赏,我找不到任何关于此的信息!

c++ static class visual-studio-2010

18
推荐指数
1
解决办法
2万
查看次数

使用pair <int,int>作为map的键

基于前一个问题,我试图使用一对整数作为关键创建一个映射,即map<pair<int, int>, int>我找到了有关如何插入的信息:

#include <iostream>
#include <map>

using namespace std;

int main ()
{
map<pair<int, int>, int> mymap;

mymap.insert(make_pair(make_pair(1,2), 3)); //edited
}   
Run Code Online (Sandbox Code Playgroud)

但我似乎无法访问该元素!我试过cout << mymap[(1,2)] << endl;但它显示错误,我找不到有关如何使用密钥访问元素的信息.难道我做错了什么?

c++ map std-pair

17
推荐指数
3
解决办法
3万
查看次数

使用循环将字符串拆分为特定长度的子单元

我需要的,如果用户它指定最大长度4为单位的字符串分割成特定的长度,例如,则循环应在原始输入运行"0123456789asdf"得到"0123","4567","89as","df".

我无法找到最好的方法 - 我需要它在循环中,因为需要在强大的每个子单元上进行进一步的处理.TIA.

编辑:我不知道原始字符串有多长,我只知道它需要变成的块的大小.此外,我需要指定长度的字符串块,以及包含字符串其余部分的最后一个块(如果它小于指定的长度).

c++ loops substr

8
推荐指数
1
解决办法
1万
查看次数

优雅地检查用户输入是否有错误

我的程序等待用户输入,并在适当时处理它.我需要检查用户输入以确保它符合某些标准,如果它不符合所有这些标准,它将被拒绝.

伪代码类似于:

if (fulfills_condition_1)
{
    if (fulfills_condition_2)
    {
        if (fulfills_condition_3)
        {
            /*process message*/
        }
        else
            cout << error_message_3; //where error_message_1 is a string detailing error
    }
    else
        cout << error_message_2; //where error_message_2 is a string detailing error
}
else
    cout << error_message_1; //where error_message_3 is a string detailing error
Run Code Online (Sandbox Code Playgroud)

这些条件的数量可能会增加,我想知道是否有一种更简洁的方式来表示使用开关或类似的东西而不是大量的级联if语句.

我知道有可能使用

if (fulfills_condition_1 && fulfills_condition_2 && fulfills_condition_3)
    /*process message*/
else
    error_message; //"this message is not formatted properly"
Run Code Online (Sandbox Code Playgroud)

但这没有第一个那么有用,也没有说明问题出在哪里.

这些条件可以粗略地安排得越来越重要,即检查condition_1比检查更重要condition_3,因此这些if语句确实有效 - 但是这样做有更好的方法吗?

c++ error-handling if-statement

5
推荐指数
1
解决办法
198
查看次数

输入sleep_until()

根据我之前的问题的答案,我正在尝试使用,sleep_until()但无法找到一个教程,告诉我如何使用实际时间,如1400:00h.

有这样的例子:std::this_thread::sleep_until(system_clock::now() + seconds(10));,但我想要的东西实际上让我指定一个时钟时间.这应该是什么格式?我会很感激任何例子.

c++ sleep

4
推荐指数
1
解决办法
2552
查看次数

使用参数初始化构造函数

阅读Andrew Koenig和Barbara E. Moo撰写的"Accelerated C++"的另一个问题,我在关于构造函数(5.1)的章节中,使用了前面的例子.

他们写

我们要定义两个构造函数:第一个构造函数不带参数并创建一个空Student_info对象; 第二个引用输入流并通过从该流中读取学生记录来初始化对象.

导致使用Student_info::Student_info(istream& is) {read(is);}第二个构造函数的例子

将实际工作委托给读取功能.[...]立即读取这些变量的新值.

Student_info堂课是

class Student_info {
public:
    std::string name() const (return n;}
    bool valid() const {return !homework.empty();}
    std::istream& read(std::istream&);

    double grade() const;
private:
    std::string n;
    double midterm, final;
    std::vector<double> homework;
};
Run Code Online (Sandbox Code Playgroud)

既然read已经定义为Student_info类下的函数,为什么需要使用第二个构造函数 - 这不是双重工作吗?为什么不使用默认构造函数,然后使用函数,因为两者都已定义?

c++ constructor class

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

需要在while循环中休息

我正在写一个循环,如果一个队列不为空,循环将运行,我只是想知道是否需要break在循环结束时包含一个.基本上每个循环应该为队列中的每个元素运行,直到队列为空.

那应该是以下哪一项 - 我只是不知道是否有正确的事情要做.

while (1)
{

    /*process message from incoming queue*/
    if (!msgs_inc.empty())
    {
        /*categorise incoming message into global map of outgoing messages*/
        msgInfo current_msg = incMsgClassification(msgs_inc.front());
        msgs_inc.pop();

        clients_msg[current_msg.destID][current_msg.priorityLevel].push(current_msg);
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

while (1)
{
    //Sleep(50000);
    //cout << "success" << endl;

    /*process message from incoming queue*/
    if (!msgs_inc.empty())
    {
        /*categorise incoming message into global map of outgoing messages*/
        msgInfo current_msg = incMsgClassification(msgs_inc.front());
        msgs_inc.pop();

        clients_msg[current_msg.destID][current_msg.priorityLevel].push(current_msg);
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ loops if-statement

0
推荐指数
1
解决办法
119
查看次数

解决编译器错误:指针可能未初始化

我需要解决一个错误,编译器捡-我明白为什么它捡了该错误,但需要解决它,因为函数(投掷)中的错误时,指针将只执行初始化.

这是我的伪代码:

if (incoming_message_exists) 
{
    msg_class* current_msg;

    /*current_msg will become either value_1 or value_2*/

    /*code block 1*/
    if (condition_is_fulfilled)
    {
        current_msg = value_1;
    }

    /*code block 2*/
    else 
    {
        current_msg = value_2;
    }

    /*code block 3*/
    /*bool function performed on current_msg that is throwing error*/
    if (function(current_msg))
    {
        //carry out function 
    }
}
Run Code Online (Sandbox Code Playgroud)

我宁愿不在1和2内执行代码块3,但如果这是唯一的解决方案,那么我会.提前致谢!

c++ if-statement

0
推荐指数
1
解决办法
101
查看次数