【问题标题】:Why am I getting all these errors in templates in c++?为什么我在 C++ 的模板中遇到所有这些错误?
【发布时间】:2020-12-23 00:26:40
【问题描述】:

这里是错误列表

错误:

 ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
  F:\C Programs\TemplateClass.cpp|39|error: invalid use of non-static data member 'arraylist<x>::cap'| F:\C
  Programs\TemplateClass.cpp|11|note: declared here| F:\C
  Programs\TemplateClass.cpp||In instantiation of 'void arraylist<x>::fill() [with x = int]':| F:\C
  Programs\TemplateClass.cpp|65|required from here| F:\C
  Programs\TemplateClass.cpp|29|error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'int*')| c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\istream|168|note:
  candidate: 'std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]' <near match>| c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\istream|168|note:  
  conversion of argument 1 would be ill-formed:| F:\C
  Programs\TemplateClass.cpp|29|error: cannot bind non-const lvalue reference of type 'bool&' to an rvalue of type 'bool'
//code

#include <iostream>
using namespace std;
#include <conio.h>

template <class x>
class arraylist {
private:
    x* ptr;
    int cap, I;

public:
    arraylist(int cap)
    {
        ptr = new x[cap];
    }
    arraylist() //default constructor
    {
    }

    void fill()
    {
        cout << "Enter " << cap << " Elements :-\n";

        for (i = 0; i < cap; i++) {
            cout << "Enter " << i + 1 << " Element : ";
            cin >> ptr + i;
            cout << endl;
        }
    }

    void replace(int index, x value) //error message
    {
        *(ptr) + index = value;
    }

    void popdata(int index = cap - 1) //error message
    {
        delete (ptr + index);
    }

    void showdata(int index)
    {
        cout << "The data at index " << index << " is " << *(ptr + index);
    }

    void showlist()
    {

        for (i = 0; i < cap; i++) {
            cout << ptr + i << endl;
        }
    }
};

int main()
{
    arraylist<int> a1;

    a1 = 10;
    a1.fill();
    a1.showlist();
    a1.replace(2, 87);
    a1.showlist();
    a1.popdata(9);
    a1.showlist();
    getch();

    return 1;
}

【问题讨论】:

  • conio.h做一点研究,简而言之,任何使用它的代码都已经过时了至少十年。作为新用户,也可以使用tour 并阅读How to Ask。无论如何,请提取并提供格式正确的minimal reproducible example
  • 你觉得a1 = 10;会做什么?
  • 建议:不要将 C++ 错误信息转成引号。将它们格式化为代码,因为通常空格很重要,而且 C++ 大量使用 会被误认为是 XML 标记。
  • @Bob__ 这实际上很好,因为它调用了构造函数。编译器可以找到带有一个参数的构造函数并进行自动转换。

标签: c++ c++11 gcc


【解决方案1】:

首先停止使用这个:

#include <conio.h>

你拥有它的唯一原因是你可以使用:

getch();

您可以将其更改为要求使用具有相同效果的输入:

#include <iostream>
...

std::cout << "Please hit return to exit application\n";
std::cin.get();

请停止使用:

using namespace std;

是的,我知道必须在几件事前添加std::,但从长远来看,这会让您的生活更轻松。


错误:

 // You have not specified the type of i
 for (i = 0; i < cap; i++) {

C++ 中的所有变量都需要一个类型。添加这样的类型:

 // Note: There are two places to fix this.
 for (int i = 0; i < cap; i++) {

您访问数组成员的语法略有偏差:

 cin >> ptr + i;
 ...
 *(ptr) + index = value;

将这些更改为:

cin >> ptr[i];
...
ptr[index] = value;

void popdata(int index = cap - 1)
                      ^^^^^^^^^^ this part is not valid
                                 outside the class (where this is being called)
                                 you don't have accesses to the members.

为了让它发挥作用,暂时去掉那部分。

void popdata(int index)

【讨论】:

  • 与其使用std::getline() 等待用户退出应用程序,不如使用std::cin.get() 代替,这将更符合OP 对getch() 的原始使用,例如:@ 987654335@
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
相关资源
最近更新 更多