无论何时我编译文件,编译器都会告诉我,我的打印未在此范围内声明。问题是我无法触摸我的主文件,因为它属于我的学校。有谁知道该怎么办?谢谢!
编译器错误消息:
main.cpp: In function ‘void TestPush()’:
main.cpp:10:5: error: ‘Print’ was not declared in this scope
Print(a);
^~~~~
在我的.h文件中:
template <typename T>
class vector
{
private:
T* v;
int count;
int capacity;
public:
vector(int capacity)
: v(new T[capacity]), count(0) {}
void Print(vector<T> s) {
for(int i = 0; i < count; i++)
{
std::cout<<v[i]<<" ";
}
std::cout<< "(size=" << size << "," << "capacity=" << capacity << ")";
}
}
在我的主文件中:
#include <iostream>
#include <cstdlib> // atoi
#include "cs150_vector.h"
void TestPush(void)
{
std::cout << "\n********** TestPush **********\n";
cs150::vector<float> a;
std::cout << "Empty array:\n";
Print(a);
std::cout << "push_back 5 floats:\n";
for (float i = 0; i < 5; i++) {
a.push_back(i);
Print(a);
}
std::cout << "pop_back until empty:\n";
while (!a.empty()) {
a.pop_back();
Print(a);
}
}