小编Rob*_*yen的帖子

程序在C++中崩溃

我刚刚开始学习C++,这是我正在为练习编写的一个程序:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int uppercase=0, lowercase=0, digits=0, other=0, i=0;
    int character;
    char* string;
    cout << "Enter a string!\n";
    cin.getline(string, 20); 

    while(true)
        {
        character = int(*(string+i));
        if (character==0)
            {
            break;
            }
        if (character > 64 && character < 91)
            {
            uppercase++;
            }
        if (character > 96 && character < 122)
            {
            lowercase++;
            }
        if (character > 47 && character <58)
            {
            digits++;
            }
        else
            {
            other++;
            }
        i++;
        }

    cout << "Upper …
Run Code Online (Sandbox Code Playgroud)

c++

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

Python中的递归和列表

这是我正在尝试做的简化版本:

class a():
    Requirement = 0
    def func(self, oldlist, x):
        newlist = [None]*3
        newlist = oldlist
        newlist[x]  = b()
        print "Class a"
        g(newlist)


class b():
    Requirement = 1


def g(list):
    for i in range(3):
        if list[i].Requirement==0:
            list[i].func(list,i)

Initiallist=[None]*3
Initiallist[0]=a()
Initiallist[1]=b()
Initiallist[2]=a()
g(Initiallist)      
Run Code Online (Sandbox Code Playgroud)

我没有试图表达我对文字的期望,而是制作了一些图表来表达我脑海中应该发生的事情:

在此输入图像描述

这意味着应该调用类a中的函数4次.但是,它只被调用2次,所以似乎发生了这种情况:

在此输入图像描述

我不明白为什么会这样或我应该如何解决它.

python recursion

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

分段故障搜索终点

我正在编写代码来计算文件的行数和字符数.

#include <fstream>
#include <iostream>
#include <stdlib.h>    
using namespace std;


int main(int argc, char* argv[])
    {
    ifstream read(argv[1]);


    char line[256];
    int nLines=0, nChars=0, nTotalChars=0;
    read.getline(line, 256);

    while(read.good())                      /
        {
        nChars=0;

        int i=0;
        while(line[i]!='\n')
            {

            if ((int)line[i]>32) {nChars++;}
            i++;
            }

        nLines++;
        nTotalChars= nTotalChars + nChars;
        read.getline(line, 256);
        }
    cout << "The number of lines is "<< nLines << endl;
    cout << "The number of characters is "<< nTotalChars << endl;
    }
Run Code Online (Sandbox Code Playgroud)

该行while(line[i]!='\n')似乎是导致以下错误的原因

分段故障(核心转储)

我无法弄清楚出了什么问题.互联网告诉我,据我所知,我正在检查线路的末端.

c++

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

将继承的方法传递给另一个方法

我正在尝试使用方法作为参数构建一个具有成员函数的类.这些方法在继承的类中定义.我构建了一个最小的例子:

#include <iostream>

struct base
{
    base() {}

    int number(int (*f)(int))
    {
        return f(1);
    }
};

struct option1 : base 
{
    int timesTwo(int i){return 2*i;}
    option1() 
    {
        std::cout << number(timesTwo);
    }
};

struct option2 : base
{
    int timesThree(int i){return 3*i;}
    int timesFour (int i){return 4*i;}
    option2() 
    {
        std::cout << number(timesThree);
    }
};

int main()
{
    option1 a; //I would expect this to print "2"
}
Run Code Online (Sandbox Code Playgroud)

函数中的当前语法number是针对一般函数的,但我无法使其适用于任何继承类的方法.

c++ methods inheritance

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

类中的类:'不是类型'错误

这是我正在尝试的简化版本

#include <iostream>
using namespace std ;

class a
    {
    public:
    a(int xinit, int yinit){x=xinit; y=yinit;}

    private:
    int x, y;
    };

class b
    {
    public:
    b(int pinit, int qinit){p=pinit; q=qinit;}

    private:
    int p,q;
    a Test(p,q);
    };

int main()
    {
    return 0;
    }
Run Code Online (Sandbox Code Playgroud)

编译时,它给出错误'p'不是一种类型.谁能告诉我什么是错的?

c++

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

无错误():下一个大小无效(快速):0x08912058

作为练习的一部分,我正在修改这个代表创建数组的不同方式的类:

template<class T>
class Array 
{
public:

Array(int size, T defaultvalue) : _size(size) 
    {
    _arr = new T[_size] ;
    _def = defaultvalue;
    }


Array(const Array& other) : _size(other._size) 
    {
    _arr = new T[other._size] ;
    // Copy elements
    for (int i=0 ; i<_size ; i++) 
        {
            _arr[i] = other._arr[i] ;
            }
    }

~Array() 
    {
        delete[] _arr ;
    }


Array& operator=(const Array& other) 
    {
    if (&other==this) return *this ;
        if (_size != other._size) 
        {
            resize(other._size) ;
            }
        for (int i=0 ; i<_size …
Run Code Online (Sandbox Code Playgroud)

c++

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

在另一个类中使用类的实例

这是我正在尝试做的简化版本

class Firstclass
    {
    public:
    Firstclass(int x)
        {
        //do things with x;
        }
    };

class Secondclass
    {
    public:
    Secondclass()
        {
        Firstclass a(10);
        }

    void func()
        {
        //Do things with a
        }

    private:
    Firstclass a;
    };
Run Code Online (Sandbox Code Playgroud)

所以我有一个类(Firstclass),带有一个带有int参数的构造函数.现在我想在另一个类(Secondclass)的构造函数中创建该类的实例.

线条

private:
Firstclass a;
Run Code Online (Sandbox Code Playgroud)

如果我只是变量而不是类,我会做什么:首先提到它,以便我可以在别处使用它(例如在函数func()中).这似乎不适用于类,因为编译器不理解Secondclass的构造函数应该做什么.

我该怎么做呢?

c++

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

在向量上调用push_back时的分段错误

我正在尝试为简单的整数行创建对象的树结构.这是我的测试用例:

#include <vector>
#include <queue>
#include <iostream>

using std::vector;
using std::queue;
using std::cout;
using std::endl;

struct Node
{
    Node(int r, int i)
      : id(i)
      , row(r)
    {}

    explicit Node(int i)
      : id(i)
      , row(0)
    {}

    Node()
      : id(0)
      , row(0)
    {}

    int nextLayer(queue<int>& queue, int& curRow)
    {
        int nextId = queue.front();
        queue.pop();

        for (int i = 0; i < children.size(); i++) {
            if (children[i].id == nextId) {
               if (children[i].row == 0)
                  return children[i].nextLayer(queue, curRow);
               else
                  return children[i].row - 1;
            } …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
2
解决办法
497
查看次数

标签 统计

c++ ×7

inheritance ×1

methods ×1

python ×1

recursion ×1