小编chr*_*ris的帖子

交叉引用类对象

我做了这样的事情:

struct Vertex {
  list<Edge*> edges;
};

struct Edge {
  Vertex* v1;
  Vertex* v2;
};
Run Code Online (Sandbox Code Playgroud)

和编译器错误:

'Edge'未在此范围内声明

如何在不将这两个标题放入单独的标题"vertex.h"和"edge.h"的情况下解决这个问题?

c++

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

是否有明确定义的随机循环终止条件?

考虑以下这个机构main:

std::srand(std::time(nullptr));
while (std::rand());
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,我无法找到任何内容,无论是在规范中,谷歌上,还是在本网站上,都是关于它是否定义明确.至于规格:

N3485§6.5/ 4 [stmt.iter]对此情况说:

[注意:迭代语句中对条件的要求在6.4中描述. - 结束说明]

但是,通过6.4,我没有看到任何涉及这种情况的东西.理论上,循环可以实际上永远持续下去,但在实践中,我通常有5毫秒的运行时间,所有测试运行中有一次是22毫秒.

将循环终止条件作为变化的(伪)随机数的基础是明确定义的行为吗?如果不是,那是什么行为?

c++ random undefined-behavior language-lawyer

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

在c ++中多次初始化数组会发生什么?

当我在c ++中多次初始化变量时,内存位置会发生什么变化?例如:

LPWSTR sampleString = new whcar_t[10];
//some operations here
sampleString = new wchar_t[2];
//some operations here
sampleString = new wchar_t[25];
//some operations here
Run Code Online (Sandbox Code Playgroud)

如果我通过使用删除内存delete [] sampleString;将清除所有相关的内存位置?

c++

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

重载+运算符,以便能够使用对象的3个const实例

我在为我的CMatrix类中的const实例定义重载的operator +时遇到了麻烦.我有这个定义为 + operator尚未 CMatrix operator+(const CMatrix &matrix) const;

它适用于non-constCMatrix的实例.但我做不到这样的事情:

const CMatrix a;
const CMatrix b;
const CMatrix c;
a=b+c;
Run Code Online (Sandbox Code Playgroud)

我在尝试编译源代码时遇到此错误: error: passing ‘const CMatrix’ as ‘this’ argument of ‘CMatrix& CMatrix::operator=(const CMatrix&)’ discards qualifiers

任何人都可以告诉我,如何定义重载以便+ operator能够编译代码?

c++ operator-overloading

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

实现将数字转换为文本的源代码(C++)

我刚开始学习C++.我正在阅读跳转到C++.问题涉及第7章问题1:

实现将数字转换为文本的源代码.

这是我到目前为止所做的:

#include <iostream>
#include <string>

using namespace std;

int LessThen20 (int i);

int main () {
    int i = 1;
    cout << "please input a number: \n";
    cin >> i;
    if (i < 20) {
        cout << LessThen20(i);
    }

    if ( i >= 20 && i < 30) {
        cout <<"Twenty " ??
    }
}

int LessThen20 (int i) {
    if (i == 0) {
        cout << "zero" <<endl;
    }

    if ( i == 1) {
        cout << …
Run Code Online (Sandbox Code Playgroud)

c++

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

函数应该有原型错误

我写了以下C程序:

#include<stdio.h>
 #include<stdlib.h>
 void main()
 {
    int count;
    scanf("%d",&count);
    if(count < 1 || count > 100)
    {
        exit(1);
    }
    int inputs[10];
    for(int i = 0; i < count; i++)
    {
        scanf("%d",&inputs[i]);
        if(inputs[i] < 1 || inputs[i] > 30000)
        {
            exit(1);
        }
    }
    for(i = 0; i < count; i++)
    {
        printPrimeFactor(inputs[i], 2);
        printf("\n");
    }
 }

 void printPrimeFactor(int number, int factor)
 {
    if(number % factor == 0)
    {
        int flag = 1, newNumber;
        newNumber = number;
        for(int i = 0; i …
Run Code Online (Sandbox Code Playgroud)

c turbo-c

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

需要帮助制作计算器

#include <stdio.h>
#include "funcs.h"

int main(void)
{
        /* varibles */
        float a[3];
        char operation;
        int num;

        /* header print */
        printf("Enter the operation you would like to use\n");
        scanf("%c", &operation);

        /* ifs */
        if(operation == "*") //warning is here
        {       
                printf("How many numbers would you like to use (max 4)\n");
                scanf("%d", &num);

                switch(num)
                {
                        case 2:
                                printf("enter your numbers\n");
                                scanf("%f", &a[0]);
                                scanf("%f", &a[1]);
                                printf(" the answer is %2f %2f", a[0] * a[1]);
                        break;
                }

        }
}
Run Code Online (Sandbox Code Playgroud)

有什么问题?我收到这个错误

Calculator.c: In function …
Run Code Online (Sandbox Code Playgroud)

c

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