小编Kud*_*aev的帖子

C++:编译器没有看到重载的运算符

我一直坚持尝试实现链表迭代器类.当我在这里使用重载的"!="运算符时编译器抱怨:

for (itr = (test0.begin()); itr != (test0.end()); ++itr)
{
    cout << *itr;
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

error: no match for ‘operator!=’ in ‘itr != SinglyLinkedList<Object>::end() [with Object = int]()’
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它找不到匹配,因为test0.end()和itr都是迭代器.

这是重载运算符的代码:

bool operator!= (iterator &rhs)
{
    return (this->current != rhs.current);
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

c++ overloading compiler-errors operator-overloading

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

C++:组合/多集函数(因子溢出)

我必须实现一个问题,该问题计算来自一组n个元素的m个元素的组合和多个集合.它们的公式如下:

在此输入图像描述

在此输入图像描述

问题是,使用阶乘很容易溢出,所以基本上解决这个问题的解决方案是什么?

由于它是TopCoder中问题的子问题,我有以下约束:

1)程序必须用C++编写.

2)我无法加载外部库.

c++ algorithm combinatorics factorial

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

书中的例子出错 - "意外的令牌{"

我正在读一本关于JavaScript的书(最近开始学习).在运行书中的一个示例时,我收到错误.我在Ubuntu上使用Chromium浏览器,14.0.835.202.

由于我是新手,我无法理解为什么会有错误.提前致谢.

Function.prototype.method = function (name, fn)
{
    this.prototype[name] = fn;
    return this;
};


var Person
{
    this.name = name;
    this.age = age;
};


Person.
    method ("getName", function
    { // error here - "Uncaught SyntaxError: Unexpected token {"
        return this.name;
    }).
    method ("getAge", function
    {
        return this.age;
    });


var alice = new Person ("Alice", 93);
var bill = new Person ("Bill", 30);

Person.
    method ("getGreeting", function
    {
        return "Hi" + this.getName() + "!";
    });

alert (alice.getGreeting());
Run Code Online (Sandbox Code Playgroud)

编辑:

解决方案给了我另一个我想问的问题.对于对象声明:

var Object …
Run Code Online (Sandbox Code Playgroud)

javascript

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

C:分配内存时混淆类型

对于以下图表声明(我无法更改 - 赋值),

#define TAG(vp)   ((vp)->tag)
#define LABEL(vp) ((vp)->label)  
#define EDGE(vp)  ((vp)->edge)

typedef struct vertex 
{
    char tag;
    char *label;
    struct vertex *edge[1];
}
vertex, *vp;       
Run Code Online (Sandbox Code Playgroud)

当我用以下行分配内存时

EDGE (test) = (vp *) malloc (sizeof (vp) * 3); // where test is a node of a graph
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

error: incompatible types when assigning to type ‘struct vertex *[1]’ from type ‘struct vertex **
Run Code Online (Sandbox Code Playgroud)

我也不能将EDGE指定为NULL.我想我遗漏了一些声明(它使用了*ptr [1]这让我很困惑).你能帮我吗?

提前致谢.

c

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

前缀和后缀增量运算符之间的差异在C++中重载迭代器的实现

我正在阅读一本关于数据结构的书,现在正试图实现单链表数据结构.在实现迭代器时,我遇到了重载前缀和后缀增量的这些实现:

iterator &operator++()
{
    this->current = this->current->next;
    return *this;
}

iterator &operator++(int)
{
    iterator old = *this;
    ++(*this);
    return old;
}
Run Code Online (Sandbox Code Playgroud)

我知道第一个是前缀,第二个是后缀,但我不明白为什么重载的后缀增量有不同的代码?如果我这样做会有什么问题?

iterator &operator++(int)
{
    this->current = this->current->next;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

c++ iterator linked-list operator-overloading

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

C++:指数中的最后一位数 - 错误的答案

我正在尝试实现一个简单的程序,它接受基数和指数并输出取幂结果的最后一位数,但在线判断说我的程序给出了错误的答案.可能有什么不对?

PS程序的约束必须是700 kb,这不是问题(我可以删除空格和注释,并使用单字母变量来避免这个问题)

#include <iostream>

using namespace std;

int main()
{
    int t; // t - number of test cases
    cin >> t;
    cin.get();
    for (int i = 0; i < t; ++i)
    {
        int base, exp; // base - base, exp - exponent
        cin >> base >> exp;
        cin.get();

        if (exp == 0)
            cout << 1 << endl;

        else if (base % 10 == 0)
            cout << 0 << endl;

        else if (base % 10 == 1)
            cout << …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm

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