【发布时间】: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__ 这实际上很好,因为它调用了构造函数。编译器可以找到带有一个参数的构造函数并进行自动转换。