Function definition does not declare parameters (Can't Find Mistake) - c++11

I am beginner in C++. So I can't find the mistake. There is a problem for "string* toolHolder" .The error message is that;
17bigthree.cpp:29:10: error: function definition does not declare parameters
string* toolHolder:
^
17bigthree.cpp: In constructor ‘GCharacter::GCharacter(std::__cxx11::string, int)’:
17bigthree.cpp:55:2: error: ‘toolHolder’ was not declared in this scope
toolHolder = new string[cap];
^
17bigthree.cpp: In copy constructor ‘GCharacter::GCharacter(const GCharacter&)’:
17bigthree.cpp:66:11: error: ‘toolHolder’ was not declared in this scope
delete []toolHolder;//g
^
17bigthree.cpp:71:24: error: ‘const class GCharacter’ has no member named ‘toolHolder’
toolHolder[i]=source.toolHolder[i];
^
17bigthree.cpp: In member function ‘GCharacter& GCharacter::operator=(const GCharacter&)’:
17bigthree.cpp:87:12: error: ‘toolHolder’ was not declared in this scope
delete []toolHolder;//g
^
17bigthree.cpp:91:25: error: ‘const class GCharacter’ has no member named ‘toolHolder’
toolHolder[i]=source.toolHolder[i];
^
17bigthree.cpp: In destructor ‘GCharacter::~GCharacter()’:
17bigthree.cpp:99:11: error: ‘toolHolder’ was not declared in this scope
delete[] toolHolder;
^
17bigthree.cpp: In member function ‘void GCharacter::insert(const string&)’:
17bigthree.cpp:107:3: error: ‘toolHolder’ was not declared in this scope
toolHolder[used]=toolName;
^
17bigthree.cpp: In function ‘std::ostream& operator<<(std::ostream&, const GCharacter&)’:
17bigthree.cpp:115:15: error: ‘const class GCharacter’ has no member named ‘toolHolder’
output<< gc.toolHolder[i] +" | ";
What kind of a problem is this? I look same problems but I can't solve the problem.
#include <iostream>
#include <string>
#include <new>
using namespace std;
class GCharacter{
friend ostream &operator<<( ostream &output, const GCharacter &gc );
public:
static const int DEFAULT_CAPACITY = 5;
GCharacter(string name = "John",int capacity= DEFAULT_CAPACITY); //constructor
GCharacter(const GCharacter& source); //copy constructor
GCharacter& operator=(const GCharacter& source); //overloaded assignment
~GCharacter(); // Destructor
void insert(const string& toolName); // insert a new tool into the tool holder
private:
string name;
int capacity;
int used;
string* toolHolder: //error1
};
int main(){
GCharacter gc1;
gc1.insert("potion");
gc1.insert("crossbow");
gc1.insert("candle");
gc1.insert("cloak");
gc1.insert("sword");
gc1.insert("book of spells");
cout<<gc1<<endl;
}
GCharacter::GCharacter(string n,int cap){ //constructor
name=n;
capacity=cap;
used=0;
toolHolder = new string[cap]; //error2
}
GCharacter::GCharacter(const GCharacter& source){ //copy constructor
cout<<"Copy Constructor Called"<<endl;
name = source.name;
capacity = source.capacity;
used = source.used;
delete []toolHolder; //error3
toolHolder = new string[capacity];
for(int i= 0; i<used; i++){
toolHolder[i]=source.toolHolder[i]; //error4
}
}
GCharacter& GCharacter::operator=(const GCharacter& source ){ //overloaded assigment operator
cout<<"Overloaded Assigment Called"<<endl;
if(this == &source){ // gc1=gc1
return *this;
}
else{
name = source.name;
capacity = source.capacity;
used = source.used;
delete []toolHolder;//g
toolHolder = new string[capacity]; //error5
for(int i= 0; i<used; i++){
toolHolder[i]=source.toolHolder[i]; //error6
}
return *this;
}
}
GCharacter::~GCharacter(){ //destructor
cout<<"Destructor Called For"<<name<<"this memory location"<<this<<endl;
delete[] toolHolder; //error7
}
void GCharacter::insert(const string& toolName){
if(used==capacity){
cout<<"Tool Holder is full.Cannot add any additional tools"<<endl;
}
else{
toolHolder[used]=toolName; //error8
used++;
}
}
ostream &operator<<( ostream &output, const GCharacter &gc ){ // overloaded ostream
output<<"Game Character"<<gc.name<<"\nhas the following tools:"<<endl<<"| ";
for(int i=0; i<gc.used;i++){
output<< gc.toolHolder[i] +" | "; //error9
}
return output<<endl;
}

Related

How to pass Comparator to user define Templeted class?

I want to create a generalized heap data structure, and facing an issue with passing template comparator.
template<typename T, typename C = less<T> > class Heap{
vector<T> *heap;
public:
Heap(vector<T> *arr){
heap = new vector<T> (arr->begin(), arr->end());
build_heap();
}
void build_heap(){
size_t n = heap->size();
for (size_t i=(n-1)/2; i>=0; i--){
shiftDown(i);
}
}
void shiftDown(size_t i){ /// heap logic
while(i < heap->size()){
size_t child = 2*i+1;
// int min_ind = 2*i+1;
if(child >= heap->size())
return;
if(child+1 < heap->size()){
if( C(heap->at(child+1),heap->at(child)) ){ // <----- using C as comparator
child++;
}
}
if( C(heap->at(child), heap->at(i)) ){ // <----- using C as comparator
swap(heap->at(child), heap->at(i));
i = child;
}
else
break;
}
}
};
int main(){
vector<int> v={8,7,6,5,4,3,2,1};
Heap<int, less<int> > heap(&v);
}
error
heap.cpp: In instantiation of ‘void Heap<T, C>::shiftDown(size_t) [with T = int; C = std::less<int>; size_t = long unsigned int]’:
heap.cpp:15:4: required from ‘void Heap<T, C>::build_heap() [with T = int; C = std::less<int>]’
heap.cpp:10:3: required from ‘Heap<T, C>::Heap(std::vector<_Tp>*) [with T = int; C = std::less<int>]’
heap.cpp:49:34: required from here
heap.cpp:32:9: error: no matching function for call to ‘std::less<int>::less(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)’
32 | if( C(heap->at(child+1),heap->at(child)) ){
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
detailed error
i'm following same syntex of declaration as stl c++ do, still i'm getting error. please help me out.
template<typename T, typename C = less<T> > class Heap;
any help or pointer to help is appreciated. thank you.
template<class T>
class Comparator{
bool operator()(const T &a, const T &b){
...
// returns logic
}
}
template<class T, class Comp >
class AnyClass{
public:
...
void function(){
// code ...
Comp<T>()(obj1, obj2);
}
...
}
calling sytex :
...
AnyClass *obj = new AnyClass<Type , Comparator>();
obj.function()
...
passing Comparator to templated class and when we need to compare objects
we create a functional object and call operator() with args to compare.
In question, that object is less<int>.
Comp<T>()(obj1, obj2);

Passing const reference pointer fails to match method signature

The following code passes a const pointer reference to a size() helper function. It only works if I remove the const or the & reference operator from the helper function.
#include <iostream>
using namespace std;
template <typename T>
class Test {
public:
Test();
int size();
void insert(T);
private:
struct Node {
T value;
Node* left;
Node* right;
};
Node* root;
int size(const Node*& node);
};
template <typename T>
Test<T>::Test() { root = nullptr;}
template <typename T>
int Test<T>::size() {return size(root);}
template <typename T>
int Test<T>::size(const Node*& node) {
if (node != nullptr)
return 1 + size(node->left) + size(node->right);
return 0;
}
int main() {
Test<int> t;
cout << "Size: " << t.size() << endl;
}
I get the following compiler errors when I compile this code as C++11:
main.cpp:31:11: error: no matching member function for call to 'size'
return size(root);
^~~~
main.cpp:43:26: note: in instantiation of member function 'Test<int>::size' requested here
cout << "Size: " << t.size() << endl;
^
main.cpp:21:11: note: candidate function not viable: no known conversion from 'Test<int>::Node *' to 'const Test<int>::Node *&' for 1st argument
int size(const Node*& node);
^
main.cpp:10:11: note: candidate function not viable: requires 0 arguments, but 1 was provided
int size();
^
1 error generated.
However, if I simply remove the const or the reference operator (&) from the helper function that size() calls, it compiles and runs exactly as expected.
In other words, either of the following works:
int size(Node*& node);
template <typename T> int Test<T>::size(Node*& node)
int size(const Node* node);
template <typename T> int Test<T>::size(const Node* node)
But this does not:
int size(const Node*& node);
template <typename T> int Test<T>::size(const Node*& node)
The declaration and implementation seem identical in all three cases, so I am having a hard time figuring out why the case with the const reference fails.
If it were legal to pass a pointer to non-const object where a reference to pointer to const object is expected, then it would be possible to violate const correctness. Consider:
const int c = 42;
void f(const int*& p) {
// Make p point to c
p = &c;
}
int* q;
f(q); // hypothetical, doesn't compile
// Now q points to c
*q = 84; // oops, modifying a const object

Function move could not be resolved, Semantic Error

The following program which is supposed to emulate std::vector. I am using Eclipse IDE for C/C++ Developers
Version: Neon.3 Release (4.6.3)
Build id: 20170314-1500
and my c++ version is g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
is flagging error that "function std::move could not be resolved".
What is the possible reason for this error ?
//============================================================================
// Name : data_structure_1.cpp
// Author : Manish Sharma
// Description : Programme to implement a simple vector class named "Vector".
// Reference : Data Structures and Algo. analysis in c++, Mark Allen Weiss
//============================================================================
#include <iostream>
#include <algorithm>
using namespace std;
template<class Object>
class Vector{
public:
// constructor
explicit Vector(int initSize = 0):
theSize{initSize},
theCapacity{initSize + SPARE_CAPACITY},
objects{new Object[theCapacity]}{
}
// copy constructor
Vector(const Vector& rhs):
theSize{rhs.theSize},
theCapacity{rhs.theCapacity},
objects{new Object[theCapacity]}{
for(int k = 0;k<theSize; ++k){
objects[k] = rhs.objects[k];
}
}
// copy assignment operaor
Vector & operator= (const Vector & rhs){
Vector copy = rhs;
std::swap(*this,copy);
return *this;
}
//class destructor
~Vector(){
delete[] objects;
}
//c++ 11 additions, reference to rvalues
Vector(Vector&& rhs) :
theSize{rhs.theSize},
theCapacity{rhs.theCapacity},
objects{rhs.objects}{
cout<<endl<<"Inside lvalue reference constructor";
//if you forget to include this then when rhs will you destroyed
//you will be left with a dangling pointer
rhs.objects = nullptr;
rhs.theSize = 0;
rhs.theCapacity = 0;
}
// copy assignment operaor
Vector & operator= (Vector && rhs){
cout<<endl<<"Inside lvalue reference copy";
Vector copy = rhs;
std::swap(*this,copy);
return *this;
}
void resize(int newSize){
if(newSize > theCapacity)
reserve(newSize*2);
theSize = newSize;
}
void reserve(int newCapacity){
if(newCapacity<theSize)
return;
Object *newArray = new Object[newCapacity];
cout<<endl<<"moving inside reserve";
for(int k=0;k<theSize;++k){
newArray[k] = std::move(objects[k]);
}
theCapacity = newCapacity;
std::swap(objects,newArray);
delete[] newArray;
}
//Some extra useful functions
int size() const{
return theSize;
}
bool empty() const{
return size()==0;
}
int capacity() const{
return theCapacity;
}
void increaseCapacity(){
reserve(2*theCapacity+1);
}
//insertion and deletion functions
void push_back(const Object & x){
if(theSize == theCapacity){
increaseCapacity();
}
cout<<endl<<"Moving inside push_back";
objects[theSize++] = std::move(x);
}
void pop_back(){
--theSize;
}
using iterator = Object*;
using const_iterator = const Object*;
iterator begin(){
return &objects[0];
}
const_iterator begin() const{
return &objects[0];
}
iterator end(){
return &objects[size()];
}
const_iterator end() const{
return &objects[size()];
}
//class specific constants
static const int SPARE_CAPACITY = 16;
private:
int theSize;
int theCapacity;
Object * objects;
};
int main() {
Vector<int> my_vector;
my_vector.push_back(10);
int j{24};
my_vector.push_back(j);
for(int i = 0;i<20;i++){
my_vector.push_back(i*10);
}
cout<<"\nSize = "<<my_vector.size()<<endl;
my_vector.capacity();
for(auto it = my_vector.begin();it!=my_vector.end();++it){
cout<<*it<<", ";
}
return 0;
}

Dynamic array inside of Class template

i have want to make template class Vector , parameters should be Type and length of an dynamic array thats in it.
template < class Type, int length >
class Vektor
{
public:
int Count;
int CurrentPos;
Type* Beginning = new Type[count];
int LastAtUse=0;
Vektor()
{
Count = length;
}
void PushBack(Type A)
{
Beginning[LastAtUse]=A;
LastAtUse++;
}
void insert(Type A, int position)
{
Beginning[position] = A;
}
};
I tried to test it in main and am getting an error:
error C2440: 'initializing' : cannot convert from 'iterator_traits<_Iter>::difference_type (__cdecl *)(_InIt,_InIt,const _Ty &)' to 'unsigned int'
Can you help me find what I'm doing wrong ?
Here:
Type* Beginning = new Type[count];
^
you have Count, not count
also, Count is not yet set when your new executes, you should move it to constructor here:
Vektor()
{
Count = length;
Beginning = new Type[Count];
}

function template specialization for inheritance

In C++11, I implement function template specialization for identifying inheritance, but it occurred compile-time errors.
f() checks whether the specified class is derived from Base or not.
Following is a source code.
#include <iostream>
#include <type_traits>
using namespace std;
struct Base {};
struct Derived : Base {};
struct Base2 {};
template<typename T, bool = std::is_base_of<Base, T>::value>
void f() {
cout << "T is not Base or Base-derived class." << endl;
};
template<typename T>
void f<T, true>() {
cout << "T is Base or Base-derived class." << endl;
};
int main() {
f<Base>(); // ok
f<Derived>(); // ok
f<Base2>(); // not ok
return 0;
}
Following is error messages.
prog.cpp:15:17: error: non-class, non-variable partial specialization 'f<T, true>' is not allowed
void f<T, true>() {
^
prog.cpp: In function 'int main()':
prog.cpp:20:13: error: call of overloaded 'f()' is ambiguous
f<Base>();
^
prog.cpp:10:6: note: candidate: void f() [with T = Base; bool <anonymous> = true]
void f() {
^
prog.cpp:15:6: note: candidate: void f() [with T = Base]
void f<T, true>() {
^
prog.cpp:21:16: error: call of overloaded 'f()' is ambiguous
f<Derived>();
^
prog.cpp:10:6: note: candidate: void f() [with T = Derived; bool <anonymous> = true]
void f() {
^
prog.cpp:15:6: note: candidate: void f() [with T = Derived]
void f<T, true>() {
^
prog.cpp:22:14: error: call of overloaded 'f()' is ambiguous
f<Base2>();
^
prog.cpp:10:6: note: candidate: void f() [with T = Base2; bool <anonymous> = false]
void f() {
^
prog.cpp:15:6: note: candidate: void f() [with T = Base2]
void f<T, true>() {
^
How can I solve it?
When std::is_base_of<Base, T>::value evaluates true you have two functions with same signature. Therefore you get error "call ... is amibguous".
Try simple overloading as one of the solutions:
namespace detail {
void doIt(std::false_type) {
cout << "T is not Base or Base-derived class." << endl;
};
void doIt(std::true_type) {
cout << "T is Base or Base-derived class." << endl;
};
}
template<typename T>
void f() {
detail::doIt(typename std::is_base_of<Base, T>::type());
};
Of course the function detail::doIt() can be more complex and templated by T.
EDIT: add "detail::" into a function f() call.

Resources