The compiler is telling me that these functions do not belong to my class or vector. I'm a bit confused as to where I could have gone wrong. Any help would be appreciated as i scramble to figure this out. I put the header for vector but that does not resolve the issue. It seems as though my compiler is failing to detect my .h file and my private variable sorted_vector.
//Compiler
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;
}
Related
a new user at coding, trying to do heap sort but got stuck.
the error I am getting is:
`heap.cpp: In member function ‘void heap::Heapsort()’:
heap.cpp: error: no matching function for call to ‘heap::swap(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)’
swap(A[0],A[i]);
^
In file included from /usr/include/c++/8/vector:64,
from heap.cpp:2:
/usr/include/c++/8/bits/stl_vector.h:1367:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::swap(std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]’
swap(vector& __x) _GLIBCXX_NOEXCEPT
^~~~
/usr/include/c++/8/bits/stl_vector.h:1367:7: note: candidate expects 1 argument, 2 provided"
Please help!!I guess there is some error in the declaration of class. I am a complete noobie and this is my first course on data structures. It would be great if someone could help.I followed the heapsort code which is in cormen for most of it but the error seems to be prevalent.
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
class heap:public vector<int>{
private:
vector<int> &A;
int length;
int heap_size;
int P(int i){return (i-1)/2;}
int le(int i){return 2*i+1;}
int ri(int i){return 2*i+2;}
public:
void maxheap(int i);
void bmh(void);
void Heapsort(void);
heap(initializer_list<int> il):
vector<int>(il), A(*this),length(A.size()),heap_size(0) {}
heap(void):A(*this),length(A.size()),heap_size(0) {}// constructor
void show(void);
};
void heap::maxheap(int i)
{
int largest;
int l=le(i),r=ri(i);
if(l<=heap_size-1)&&A[l]>A[i])
largest=l;
else
largest=i;
if(r<=heap_size-1&&A[r]>A[i])
largest=r;
else
largest=i;
if(largest!=i){
swap(A[largest],A[i]);
maxheap(largest);
}
};
void heap::bmh(void)
{
heap_size=length;
for(int i=length/2;i>=0;i--)
{
maxheap(i);
}
}
void heap::Heapsort(void)
{
bmh();
for(int i=length-1;i>0;i--){
swap(A[0],A[i]);
heap_size=heap_size-1;
maxheap(i);
}
}
void heap::show(void)
{
for(int i=0;i<length-1;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
}
int main()
{
heap h<int>={16,4,10,14,7,9,3,2,8,1};
h.show();
//h.Build_Max_Heap();
//h.show();
// Test the member functions of heap class.
h.Heapsort();
h.show();
}
std::vector<int> has a member named swap. This member hides the global function std::swap. You can access it via its fully qualified name.
Besides, you haven't included either <algorithm> or <utility>, so it's possible std::swap isn't even declared.
I'm dealing with an issue right now in C++, where I have a defined variable existing within my class definition, the compiler gets angry and tells me that said variable has not been defined within the scope, and that the constructor doesn't have any field for the variable in question.
I have a simple class hierarchy where I have the base class, Neuron, and two subclasses HiddenNeuron and OutputNeuron, that are both inheriting from Neuron in public virtual mode.
I've made a neuron.h file where I defined the virtual header class, and as such, I have been told by my instructor that I need not write an implementation file for it.
I've then written two header files for HiddenNeuron and OutputNeuron, in which I define them, and two corresponding cpp files that implement them, respectively.
When I try running
g++ neuralnet.cpp input_neuron.cpp hidden_neuron.cpp output_neuron.cpp
I get a bunch of errors like:
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
and
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
Here's the code for my classes:
neuron.h:
#ifndef NEURON_H
#define NEURON_H
class Neuron {
//protected:
// double long value;
public:
Neuron(void);
//Neuron(const double long val);
//
virtual double long activation(void) const =0;
virtual void input(double long val);
};
#endif
output_neuron.h:
#ifndef OUTPUT_NEURON_H
#define OUTPUT_NEURON_H
#include "neuron.h"
class OutputNeuron : public virtual Neuron {
private:
double long value;
public:
OutputNeuron(void);
OutputNeuron(const double long val);
// this should return a value between 0 & 1
virtual double long activation(void) const;
// vector multiplication of the weight vector
// and all the values of the last layer
// with an additional corresponding bias
// added onto the end.
virtual void input(double long val);
};
#endif
output_neuron.cpp:
#include "output_neuron.h"
#include <cmath>
OutputNeuron::OutputNeuron(void) : value(0) { }
OutputNeuron::OutputNeuron(const double long val)
: value(val) { }
inline double long OutputNeuron::activation(void) const {
return 1 / (1 + exp((-1)*value));
}
inline void OutputNeuron::input(double long val) {
value = val;
}
hidden_neuron.h:
#ifndef HIDDEN_NEURON_H
#define HIDDEN_NEURON_H
#include "neuron.h"
class HiddenNeuron : public virtual Neuron {
private:
double long value;
public:
HiddenNeuron(void);
HiddenNeuron(const double long val);
// activation function that takes the stored value
// and squashes it and returns it
virtual double long activation(void) const;
virtual void input(double long val);
};
#endif
hidden_neuron.cpp:
#include "hidden_neuron.h"
#include <cstdlib>
#include <cmath>
HiddenNeuron::HiddenNeuron(void)
: value(pow(-1, rand()) * static_cast<double>(rand())/static_cast<double>(RAND_MAX)){}
HiddenNeuron::HiddenNeuron(const double long val) : value(val) { }
inline void HiddenNeuron::input(double long val) { value = val; }
inline double long HiddenNeuron::activation(void) const {
return 1/(1 + exp((-1)*value));
}
FULL ERROR LOG:
neuralnet.cpp: In member function ‘void NeuralNet::initialize(bool)’:
neuralnet.cpp:72:18: error: invalid new-expression of abstract class type ‘HiddenNeuron’
temp = new HiddenNeuron;
^~~~~~~~~~~~
In file included from neuralnet.h:5:0,
from neuralnet.cpp:1:
hidden_neuron.h:5:7: note: because the following virtual functions are pure within ‘HiddenNeuron’:
class HiddenNeuron : public virtual Neuron {
^~~~~~~~~~~~
In file included from hidden_neuron.h:3:0,
from neuralnet.h:5,
from neuralnet.cpp:1:
neuron.h:14:16: note: virtual void Neuron::input(long double) const
virtual void input(double long val) const =0;
^~~~~
neuralnet.cpp:85:18: error: invalid new-expression of abstract class type ‘HiddenNeuron’
temp = new HiddenNeuron;
^~~~~~~~~~~~
neuralnet.cpp:98:18: error: invalid new-expression of abstract class type ‘OutputNeuron’
temp = new OutputNeuron;
^~~~~~~~~~~~
In file included from neuralnet.h:7:0,
from neuralnet.cpp:1:
output_neuron.h:5:7: note: because the following virtual functions are pure within ‘OutputNeuron’:
class OutputNeuron : public virtual Neuron {
^~~~~~~~~~~~
In file included from hidden_neuron.h:3:0,
from neuralnet.h:5,
from neuralnet.cpp:1:
neuron.h:14:16: note: virtual void Neuron::input(long double) const
virtual void input(double long val) const =0;
^~~~~
hidden_neuron.cpp: In constructor ‘HiddenNeuron::HiddenNeuron()’:
hidden_neuron.cpp:6:5: error: class ‘HiddenNeuron’ does not have any field named ‘value’
: value(pow(-1, rand()) * static_cast<double>(rand())/static_cast<double>(RAND_MAX)){}
^~~~~
hidden_neuron.cpp: In constructor ‘HiddenNeuron::HiddenNeuron(long double)’:
hidden_neuron.cpp:8:53: error: class ‘HiddenNeuron’ does not have any field named ‘value’
HiddenNeuron::HiddenNeuron(const double long val) : value(val) { }
^~~~~
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
^~~~~
hidden_neuron.cpp:10:52: note: suggested alternative: ‘val’
inline void HiddenNeuron::input(double long val) { value = val; }
^~~~~
val
hidden_neuron.cpp: In member function ‘virtual long double HiddenNeuron::activation() const’:
hidden_neuron.cpp:13:26: error: ‘value’ was not declared in this scope
return 1/(1 + exp((-1)*value));
^~~~~
hidden_neuron.cpp:13:26: note: suggested alternative: ‘valloc’
return 1/(1 + exp((-1)*value));
^~~~~
valloc
output_neuron.cpp: In constructor ‘OutputNeuron::OutputNeuron()’:
output_neuron.cpp:4:36: error: class ‘OutputNeuron’ does not have any field named ‘value’
OutputNeuron::OutputNeuron(void) : value(0) { }
^~~~~
output_neuron.cpp: In constructor ‘OutputNeuron::OutputNeuron(long double)’:
output_neuron.cpp:7:5: error: class ‘OutputNeuron’ does not have any field named ‘value’
: value(val) { }
^~~~~
output_neuron.cpp: In member function ‘virtual long double OutputNeuron::activation() const’:
output_neuron.cpp:10:28: error: ‘value’ was not declared in this scope
return 1 / (1 + exp((-1)*value));
^~~~~
output_neuron.cpp:10:28: note: suggested alternative: ‘valloc’
return 1 / (1 + exp((-1)*value));
^~~~~
valloc
output_neuron.cpp: In member function ‘virtual void OutputNeuron::input(long double)’:
output_neuron.cpp:14:3: error: ‘value’ was not declared in this scope
value = val;
^~~~~
output_neuron.cpp:14:3: note: suggested alternative: ‘val’
value = val;
^~~~~
val
If anyone can help me, I've been pulling my hair out with this issue.
Edit: using class design as it's a requirement for the course I'm currently enrolled in.
Edit 2: included the full error log
Edit 3: Okay so this is really bizarre, what I wrote works in a separate project directory as pointed out by user idontseethepoint, however when it's in the same directory as my main project, it still gives me the exact same error.
What is the problem with the following code. Because if I define the class inside the main function, the compilation fails and I don't understand the compiler error.
Test the code from here
Comment the 1st definition of drift_f (outside of main()) and uncomment the inner definition of drif_t (inside the main() function) and the compiler will get the following error message:
prog.cpp: In function ‘int main()’:
prog.cpp:27:24: error: template argument for ‘template<class> class std::allocator’ uses local type ‘main()::drift_t’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:24: error: trying to instantiate ‘template<class> class std::allocator’
prog.cpp:27:24: error: template argument 2 is invalid
prog.cpp:27:31: error: invalid type in declaration before ‘;’ token
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:39: error: request for member ‘push_back’ in ‘drift’, which is of non-class type ‘int’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:69: error: request for member ‘push_back’ in ‘drift’, which is of non-class type ‘int’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
#include <iostream>
#include <deque>
using namespace std;
class drift_t{ //It works
public:
int _drift;
int _immediateDrift;
drift_t() : _drift(0), _immediateDrift(0) {}
drift_t(int d, int expected) : _drift(d), _immediateDrift(expected) {}
drift_t(const drift_t& ro) : _drift(ro._drift), _immediateDrift(ro._immediateDrift) {}
drift_t& operator = (const drift_t& ro) { this->_drift = ro._drift; this->_immediateDrift = ro._immediateDrift; return *this; }
} ;//*/
int main() {
/*class drift_t{ //It doesn't works
public:
int _drift;
int _immediateDrift;
drift_t() : _drift(0), _immediateDrift(0) {}
drift_t(int d, int expected) : _drift(d), _immediateDrift(expected) {}
drift_t(const drift_t& ro) : _drift(ro._drift), _immediateDrift(ro._immediateDrift) {}
drift_t& operator = (const drift_t& ro) { this->_drift = ro._drift; this->_immediateDrift = ro._immediateDrift; return *this; }
} ;//*/
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
return 0;
}
Isn't the error the one that the compiler says it is? You can't use the local class for that template initialization.
Try compiling with -std=c++11 as I believe it relaxes on that.
Hi I am making a simple stack class in C++ and am new to C++. I am having a few errors that I cannot figure out what they mean. Some help would be greatly appreciated! Here is my code:
Stack.h
#ifndef SStack
#define SStack
#include <cstdlib>
#include <string>
class SStack
{
public:
// Constructor
SStack( int cap);
// Copy Constructor
SStack( const SStack& s );
~SStack( );
void push ( const std::string& s);
std::string& pop ();
std::string& top () const;
bool IsEmpty () const;
int size() const;
int getCapacity() const;
// NONMEMBER FUNCTIONS for the bag class
// Precondition: s1.size( ) + s2.size( ) <= s1.Capacity.
// Postcondition: The stack returned is the union of s1 and s2.
SStack operator +(const SStack& s2);
private:
int Capacity; // Capacity is the maximum number of items that a stack can hold
std::string *DynamicStack;
int used; // How many items are stored in the stack
};
#endif
Stack.cpp
#include "SStack.h"
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
class SStack
{
public:
void SStack(int cap){
DyanmicStack = new string[cap];
Capacity = cap;
used = -1;
}
void SStack(const SStack& s){
DyanmicStack = new string[cap];
}
~SStack( ){
delete(DynamicStack);
}
void push(const string& s){
DynamicStack[used] = s;
used++;
}
string& pop(){
if(used==-1){
cout << "Error stack is empty";
return " ";
}
else{
used--;
return DynamicStack[used+1];
}
}
string& top () const{
if(used==-1){
cout << "Error stack is empty";
return " ";
}
else{
return DynamicStack[used];
}
}
bool isEmpty(){
return (used==-1);
}
int size(){
return (used+1);
}
int getCapacity(){
return Capacity;
}
private:
int Capacity; //How much the stack can hold
string* DynamicStack;
int used; //objects in the stack
};
And here are the errors:
SStack.h:11: error: expected unqualified-id before ‘int’
SStack.h:11: error: expected `)' before ‘int’
SStack.h:13: error: expected unqualified-id before ‘const’
SStack.h:13: error: expected `)' before ‘const’
SStack.h:14: error: expected class-name before ‘(’ token
SStack.h:25: error: ISO C++ forbids declaration of ‘operator+’ with no type
SStack.h:25: error: ISO C++ forbids declaration of ‘s2’ with no type
SStack.h:8: error: an anonymous union cannot have function members
SStack.h:31: error: abstract declarator ‘<anonymous class>’ used as declaration
SStack.cpp:11: error: expected unqualified-id before ‘int’
SStack.cpp:11: error: expected `)' before ‘int’
SStack.cpp:17: error: expected unqualified-id before ‘const’
SStack.cpp:17: error: expected `)' before ‘const’
SStack.cpp:21: error: expected class-name before ‘(’ token
SStack.cpp: In member function ‘std::string&<anonymous class>::pop()’:
SStack.cpp:33: error: invalid initialization of non-const reference of type ‘std::string&’ from a temporary of type ‘const char*’
SStack.cpp: In member function ‘std::string&<anonymous class>::top() const’:
SStack.cpp:44: error: invalid initialization of non-const reference of type ‘std::string&’ from a temporary of type ‘const char*’
SStack.cpp: At global scope:
SStack.cpp:8: error: an anonymous union cannot have function members
SStack.cpp:70: error: abstract declarator ‘<anonymous class>’ used as declaration
Your include guard has the same name as your class. Remember that the preprocessor is a very simplistic search/replace feature.
#define SStack
class SStack
{
public:
// Constructor
SStack( int cap);
becomes:
#define SStack
class
{
public:
// Constructor
( int cap);
One pattern is to name it the same as your header's filename, such as STACK_H.
First of all, as was already noted, the header guard is broken.
Now, onto the actual problems:
You have misunderstood how class definition works. Your header is largely correct, what is wrong is your .cpp file. You are redefining class that you already have defined inside the header file. The proper way to provide implementation of member function is this
void SStack::SStack(const SStack& s){
DyanmicStack = new string[cap];
} or in for clearer example:
void SStack::push(const string& s){
DynamicStack[used] = s;
used++;
}.
Basically, you have to prepend the classes's name before the function name.
Also, just by copy pasting this I've noticed typo in your code (see if you can spot it ;-) ), and I would recommend rethinking the design, even if it is just an exercise.
I realized the class Pila(stack) some times ago, this is my solution:
(sorry but I'm new.. so I don't know how to indent the code here)
file: pila.h
//nodo is the type of elements that class pila contains.
struct nodo
{
int dato;
nodo* precedente;
};
class pila
{
private:
nodo* ultimo;
public:
pila();
void push(int numero);
int pop();
};
file: pila.cpp
#include <iostream>
#include"pila.h"
using namespace std;
pila::pila()
{
ultimo=NULL; // punta all'ultimo nodo inserito
}
void pila::push(int numero)
{
nodo* nuovo;
nuovo=new struct nodo;
nuovo->dato=numero;
if(ultimo==NULL)
{
nuovo->precedente=NULL;
ultimo=nuovo;
}
else
{
nuovo->precedente=ultimo;
ultimo=nuovo;
}
}
int pila::pop()
{
if (ultimo==NULL)
{
return 0;
}
else
{
int prelevato=ultimo->dato;
ultimo=ultimo->precedente;
return prelevato;
}
}
I have written a simple program.
I am getting this error:
time.cpp: In function ‘int main()’:
time.cpp:22:9: error: expected ‘;’ before ‘a’
time.cpp:23:4: error: ‘a’ was not declared in this scope
time.cpp:24:4: error: ‘b’ was not declared in this scope
time.cpp:25:4: error: ‘c’ was not declared in this scope
This is my code:
#include<iostream>
using namespace std;
class time
{
int hour;
int min;
public:
void gettime(int h,int m)
{
hour=h;
min=m;
}
void puttime()
{
cout<<hour<<endl<<min;
}
void sum(time x,time y)
{
min=x.min+y.min;
hour=min/60;
min=min%60;
hour=hour+x.hour+y.hour;
}
};
int main()
{
time a,b,c;
a.gettime(2,45);
b.gettime(3,35);
c.sum(a,b);
a.puttime();
b.putime();
c.puttime();
return 0;
}
Remember that there is a standard function named time.
This is the one main reason you should refrain from using namespace std;.
b.putime() must be b.puttime() here. Otherwise this code compiled