zoukankan      html  css  js  c++  java
  • 剑指offer55 字符流中第一个不重复的字符(最典型错误)

    典型并且基础的错误:

    class Solution
    {
    public:
      //Insert one char from stringstream
        void Insert(char ch)
        {
            if(result[ch] == -1)
                result[ch] = index;
            else if(result[ch] >= 0)
                result[ch] = -2;
            index++;
        }
      //return the first appearence once char in current stringstream
        char FirstAppearingOnce()
        {
            char ch = '';
            int minindex = 999;
            for(int i = 0;i < 256;i++){
                if(result[i] >= 0){
                    if(result[i] < minindex){
                        minindex = result[i];
                        ch = (char)i;
                    }
                }
            }
            return ch;
        }
        int result[256];
        for(int i = 0;i < 256;i++)
            result[i] = -1;
        int index = 0;
    };

    错误显示:

    编译错误:您提交的代码无法完成编译
    In file included from a.cc:2:
    ./solution.h:29:5: error: expected member name or ';' after declaration specifiers
    for(int i = 0;i < 256;i++)
    ^~~
    a.cc:13:19: warning: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Wsign-compare]
    for(int i = 0;i < strlen(str);i++) {
    ~ ^ ~~~~~~~~~~~
    1 warning and 1 error generated.

    c++的类中只能有属性和方法(也可以说函数)。

    方法就是你定义的类中的那些实现函数,属性相当于类中的那些参数。

    上面代码中报错的for循环,目的是对result数组进行初始化,但这个for循环不是一个函数,在类里面是不允许这种执行语句的,必须转换成函数才行。因为result本身是这个class类的属性,所以对result的初始化,其实相当于对类属性的初始化,可以通过构造函数来实现。

    构造函数本身就是实现初始化类对象的数据成员。

  • 相关阅读:
    CSS的应用
    关于新手html的认识 以及对table的基本用法
    javascript的使用方法
    CSS的使用方式和选择器的用法
    html基础知识点
    前端课堂第四课
    前端课堂第三课
    前端实训第二课
    前端实训随笔
    JS02
  • 原文地址:https://www.cnblogs.com/ymjyqsx/p/7245726.html
Copyright © 2011-2022 走看看