Компилятор сообщает мне, что эти функции не принадлежат моему классу или вектору. Я немного не понимаю, где я мог ошибиться. Любая помощь будет оценена по достоинству, поскольку я пытаюсь понять это. Я поместил заголовок для вектора, но это не решило проблему. Кажется, что мой компилятор не может обнаружить мой файл .h и мою частную переменную sorted_vector.
// Компилятор
In file included from SortedVector.cpp:1:0:
SortedVector.h:22:3: error: ‘vector’ does not name a type
vector<int> sorted_vector;
^~~~~~
SortedVector.cpp: In constructor ‘SortedVector::SortedVector(int)’:
SortedVector.cpp:5:2: error: ‘sorted_vector’ was not declared in this scope
sorted_vector.reserve(cap);
^~~~~~~~~~~~~
SortedVector.cpp: In member function ‘int SortedVector::getCapacity() const’:
SortedVector.cpp:9:9: error: ‘sorted_vector’ was not declared in this scope
return sorted_vector.capacity();
^~~~~~~~~~~~~
SortedVector.cpp: In member function ‘int SortedVector::getSize() const’:
SortedVector.cpp:13:9: error: ‘sorted_vector’ was not declared in this scope
return sorted_vector.size();
^~~~~~~~~~~~~
SortedVector.cpp: In member function ‘bool SortedVector::isEmpty() const’:
SortedVector.cpp:17:6: error: ‘sorted_vector’ was not declared in this scope
if (sorted_vector.empty())
^~~~~~~~~~~~~
SortedVector.cpp: In member function ‘void SortedVector::insertVal(int)’:
SortedVector.cpp:25:8: error: ‘sorted_vector’ was not declared in this scope
if(sorted_vector.isEmpty())
^~~~~~~~~~~~~
SortedVector.cpp:28:8: error: ‘sorted_vector’ was not declared in this scope
if(sorted_vector.isEmpty()){
^~~~~~~~~~~~~
SortedVector.cpp: At global scope:
SortedVector.cpp:39:5: error: prototype for ‘int SortedVector::find(int)’ does not match any in class ‘SortedVector’
int SortedVector::find(int val) {
^~~~~~~~~~~~
In file included from SortedVector.cpp:1:0:
SortedVector.h:14:7: error: candidate is: int SortedVector::find(int) const
int find(int val) const;
^~~~
SortedVector.cpp: In member function ‘bool SortedVector::deleteVal(int)’:
SortedVector.cpp:50:2: error: ‘vector’ was not declared in this scope
vector<int> temp;
^~~~~~
SortedVector.cpp:50:2: note: suggested alternative:
In file included from /usr/include/c++/6/vector:64:0,
from SortedVector.h:6,
from SortedVector.cpp:1:
/usr/include/c++/6/bits/stl_vector.h:214:11: note: ‘std::vector’
class vector : protected _Vector_base<_Tp, _Alloc>
^~~~~~
SortedVector.cpp:50:9: error: expected primary-expression before ‘int’
vector<int> temp;
^~~
SortedVector.cpp:51:2: error: ‘temp’ was not declared in this scope
temp.reserve(sorted_vector.capacity());
^~~~
SortedVector.cpp:51:15: error: ‘sorted_vector’ was not declared in this scope
temp.reserve(sorted_vector.capacity());
^~~~~~~~~~~~~
SortedVector.cpp:75:1: error: expected ‘}’ at end of input
}
// SortedVector.h
#ifndef SORTEDVECTOR_H_
#define SORTEDVECTOR_H_
#include <iostream>
using std::ostream;
#include <vector>
class SortedVector {
public:
SortedVector();
SortedVector(int cap = 10);
void insertVal(int val);
int find(int val) const;
bool deleteVal(int val);
bool isEmpty() const;
friend ostream & operator<<(ostream & out, const SortedVector & sV);
int getCapacity() const;
int getSize() const;
private:
vector<int> sorted_vector;
};
#endif
// SortedVector. cpp
#include "SortedVector.h"
#include <vector>
SortedVector::SortedVector(int cap) {
sorted_vector.reserve(cap);
}
int SortedVector::getCapacity() const{
return sorted_vector.capacity();
}
int SortedVector::getSize() const{
return sorted_vector.size();
}
bool SortedVector::isEmpty() const{
if (sorted_vector.empty())
return true;
else
return false;
}
void SortedVector::insertVal(int val) {
if(sorted_vector.isEmpty())
sorted_vector.push_back(val);
if(sorted_vector.isEmpty()){
for(int i=0;i!=sorted_vector.size();i++){
if(sorted_vector[i]<=val)
sorted_vector.insertVal(val);
}
}
}
int SortedVector::find(int val) {
for(int i=0; i<sorted_vector.size(); i++){
if(sorted_vector[i]==val)
return i;
else
return -1;
}
}
bool SortedVector::deleteVal(int val) {
vector<int> temp;
temp.reserve(sorted_vector.capacity());
temp = sorted_vector;
int idx = sorted_vector.find(val)
if (idx!= -1 ){
for(int i =0; i<sorted_vector.size(); i++){
if(i==idx)
continue;
temp.push_back(sorted_vector[i]);
}
sorted_vector=temp;
return true
}
else
return false;
}
ostream & operator<<(ostream & out, const SortedVector & sV) {
for(int i = 0; i < sV.getSize(); i++) {
out << sV.sorted_vector[i] << " ";
}
return out;
}