简体   繁体   English

为什么在c ++中会显示“找不到构造函数”?

[英]Why does it say “can't find constructor” in c++?

#include <iostream>

using namespace std;

template <class T>
class ring {
private:
    T *m_values;
    int m_size;
    int m_index;

public:
    class iterator;

public:
    ring(int size):
            m_size(size), m_index(0), m_values(NULL) {
        m_values = new T[size];
    }

    ~ring() {
        delete [] m_values;
     }

    int size() const {
        return m_size;
    }

    iterator begin() {
        return iterator(0, *this);
    }

    iterator end() {
        return iterator(m_size, *this);
    }

    void add(T value) {
        m_values[m_index] = value;
        m_index = (m_index + 1) % m_size;
    }

    T &get(int pos) {
        return m_values[pos];
    }
};

template<class T>
class ring<T>::iterator {
private:
     int m_pos;
     ring &m_ring;
public:
    iterator(int m_pos, ring &m_ring): m_pos(m_pos), m_ring(m_ring) {};

    iterator &operator++(int) {
        m_pos++;
        return *this;
    }

    iterator &operator++() {
        m_pos++;
        return *this;
    }

    bool operator!=(const iterator &other) {
        return other.m_pos != m_pos;
    }

    T &operator*() {
        return m_ring.get(m_pos);
    }
};

Here is the c++ code. 这是c ++代码。 I am new to c++ and this codes at CLion gives me "Class iterator doesn't have a constructor iterator(int,ring)" at begin() and end() function. 我是C ++的新手,CLion的这段代码在begin()和end()函数中给我“类迭代器没有构造函数iterator(int,ring)”。 Can anone give me some hint about why this would happen even I have defined it? 有人能给我一些暗示,为什么即使我定义了它也会发生这种情况?

BTW: This is from udemy "Learn Advanced C++ Programming" lecture 44. 顺便说一句:这是来自udemy的“学习高级C ++编程”讲座44。

UPDATE: Some comments find I didn't define and only declare. 更新:一些评论发现我没有定义,仅声明。 That's not the reason because I defined it in a separate .cpp file. 这不是原因,因为我在单独的.cpp文件中定义了它。 I also tried to define it inline but still give me the same error message from CLion. 我也尝试内联定义它,但仍然给我来自CLion的相同错误消息。

You declare the constructor here 您在这里声明构造函数

iterator(int m_pos, ring &m_ring);

but there is no definition of this special member function. 但没有定义此特殊成员函数。 If you provide one, you're probably fine 如果您提供一个,那可能很好

iterator(int m_pos, ring &m_ring) : m_pos(m_pos), m_ring(m_ring)
{
    /* Maybe some other stuff... */
}

You declared the interator(int, ring) but not defined it. 声明了interator(int,ring),但未定义它。 That is the problem. 那就是问题所在。

Declaration vs Definition : 声明与定义

A declaration provides basic attributes of a symbol: its type and its name. 声明提供符号的基本属性:符号的类型和名称。 A definition provides all of the details of that symbol--if it's a function, what it does; 定义提供了该符号的所有详细信息-如果它是一个函数,它将做什么? if it's a class, what fields and methods it has; 如果是一个类,它具有哪些字段和方法; if it's a variable, where that variable is stored. 如果是变量,则存储该变量。 Often, the compiler only needs to have a declaration for something in order to compile a file into an object file, expecting that the linker can find the definition from another file. 通常,编译器只需要声明某些内容即可将文件编译为目标文件,并期望链接程序可以从另一个文件中找到该定义。 If no source file ever defines a symbol, but it is declared, you will get errors at link time complaining about undefined symbols. 如果没有源文件定义符号,但已声明它,则在链接时抱怨未定义符号会出错。

src: https://www.cprogramming.com/declare_vs_define.html src: https//www.cprogramming.com/declare_vs_define.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM