identifier "fill_vector" is undefinded and im not sure why - c++

int miniVector<T>::fill_vector(miniVector<T> &obj)
{
int numofelements;
cout << "For how many objects do you want to enter value<s>? ";
cin >> numofelements;
obj.resize(numofelements);
int i;
for(i = 0; i < numofelements; i++)
{
cin >> obj[i];
obj.push_back(i);
}
return i;
}
int main()
{
miniVector<int> v;
cout << "int vector: " << endl;
//fill_vector();
int sizearray;
sizearray = fill_vector<int>(v);
sizearray = v.size();
//display_vector(v, sizearray);
return 0;
}
Was wonder why i keep getting an error message idnetifier "fill_vector is undefined.

int miniVector<T>::fill_vector(miniVector<T> &obj)
This says: a method of a class miniVector of type T, that returns an int and takes in a reference to a miniVector object of type T.
You cannot call this as you have, using
sizearray = fill_vector<int>(v);
You would have to call it like this:
sizearray = v.fill_vector(some_other_miniVector);
where
some_other_miniVector is
miniVector<int> some_other_miniVector;

fill_vector is a member function of class miniVector. Thus you have to specify an object for which the member function is called in this statement (if the member function is non-static) or class name (if the member function is static)
sizearray = fill_vector<int>(v);
This call is wrong also because the method itself is not template.
So there is no definition of template function fill_vector and the compiler issues an error.

Related

Calling a Function in a Class with Parameters C++

I'm practicing in order to better understand dynamic arrays and using them in a class. However, I'm struggling to call my functions inside the class. I have no issue with my int size variable, but my int myArray variable is giving me problems. I get the error "expected a member name" when I try to call my void functions in my main function. Are arrays not allowed to be used in this situation?
#include <iostream>
using namespace std;
class myClass
{
public:
int size;
int* myArray = new int[size];
void storeData(int& size, int (&myArray)[]);
void printData(int& size, int(&myArray)[]);
};
void myClass::storeData(int& size, int(&myArray)[])
// Stores array data.
{
cout << "Enter Size of the array: ";
cin >> size;
// User determines array size.
for (int x = 0; x < size; x++)
{
cout << "Array[" << x << "]: ";
cin >> myArray[x];
// User determines array values.
cout << endl;
}
}
void myClass::printData(int &size, int(&myArray)[])
// Displays values of the array.
{
cout << "Value of the arrays are: ";
for (int x = 0; x < size; x++)
{
cout << myArray[x] << " ";;
}
delete[]myArray;
}
int main()
{
myClass object;
object.storeData(object.size, object.(&myArray)[]);
// E0133 expected a member name.
object.printData(object.size, object.(&myArray)[]);
// E0133 expected a member name.
}
There are a couple issues here, I will try to address all of them.
When passing an array to a function, never use [] syntax. In C and C++, arrays decay to pointers, so we do not need [] nor &.
This is valid syntax to pass an array:
int my_array [] = {1,2,3,4};
my_function(my_array, 4);
...
void my_function(int * array, size_t size)
{
//Iterate over the array or do something...
}
In addition, if a function exists within a class, it can access class members freely, meaning we do not have to pass them in at all. See the following change to your code:
void myClass::storeData(int size)
// Stores array data. We do NOT need a pointer to the object array, we already have it!
{
cout << "Enter Size of the array: ";
cin >> size;
// User determines array size.
for (int x = 0; x < size; x++)
{
cout << "Array[" << x << "]: ";
cin >> myArray[x];
// User determines array values.
cout << endl;
}
}
Lastly, dynamic sized arrays must be allocated dynamically. Do not use int* myArray = new int[size]; in your class definition, because size is not yet initialized. Instead, use a constructor or use your store_data function to allocate the memory.
class myClass
{
public:
size_t size;
int * myArray; //Do not allocate anything here...
myClass(size_t size)
{
this->size = size;
myArray = new int[size];
}
};
You can get the size however you want, via user input, etc. and pass this to the constructor or an allocator function like storeData.

When Declaring a Function in C++, Does It Need to Have the Parameters That the Function Itself Has?

(Please keep in mind I've only recently delved into C++ functions.)
Let's say that you want a function that will count from 1 to a specific number.
#include <iostream>
int countTo(int num);
int countTo(int num) {
for (int i = 1; i <= num; i++) {
std::cout << i << "\n";
}
return num;
}
int main() {
int num;
std::cout << "Enter a number to which the program will count: ";
std::cin >> num;
countTo(num);
return 0;
}
I've put the same code into the compiler, just without the parameters on the function declaration, like so:
int countTo();
int countTo(int num) {
for (int i = 1; i <= num; i++) {
std::cout << i << "\n";
}
return num;
}
And it worked just as well. Do I need to include parameters when declaring int countTo(int num)? Or for any function?
std::cout << "Thanks!!";
There are two different things going on when you write this:
int countTo(int num) {
for (int i = 1; i <= num; i++) {
std::cout<<i<<"\n";
}
return num;
}
You are declaring a function called countTo that takes an int and returns an int, and you are also defining the function.
When you have the line above it saying
int countTo(int num);
you are declaring the same function, but not defining it.
When you changed that line to
int countTo();
you declared a different function (an overload) that takes no parameters. It doesn't matter that you didn't define that function, because no one ever tried to call it.

Simple 2D Array header file, print fails

I am writing a simple header file that can make a dynamic 2d array and put 0 in the row and col, print the array, and delete the array.
In Debug when stepping through, the 2d array gets initialized, it puts 0 in there, however when my_array.Print_Array(); called, the compiler skips it.
when i try to print the array from the main file it fails. any help would be appreciate it.
HEADER FILE:
class twoD_Array
{
public:
int **Array;
int *col, *row;
int size_col, size_row;
twoD_Array(int, int);
~twoD_Array();
void Print_Array();
};
twoD_Array::twoD_Array(int size_c, int size_r)
{
size_col = size_c;
size_row = size_r;
Array = new int *[size_col];
for (int i = 0; i < size_col; i++)
{
Array[i] = new int[size_row];
for (int j = 0; j < size_row; j++)
Array[i][j] = 0;
}
}
void twoD_Array::Print_Array()
{
for (int y_i = 0; y_i<size_col; y_i++)
{
for (int x_i = 0; x_i<size_col; x_i++)
std::cout << Array[y_i][x_i];
std::cout << std::endl;
}
}
twoD_Array::~twoD_Array()
{
for (int i = 0; i < size_row; i++)
delete[] Array[i];
delete[] Array;
}
Main File:
#include "stdafx.h"
#include <iostream>
#include "2D_Array.h"
int main()
{
int x, y;
std::cout << "how many x variables?" << std::endl;
std::cin >> x;
std::cout << "how many y variables?" << std::endl;
std::cin >> y;
twoD_Array my_array(x, y);
my_array.Print_Array();
return 0;
}
You are using the local variables in the constructor, but you're using the member variables in the Print_Array method, which are not initialized. You need to initialize the member variables size_col and size_row in the constructor where they are provided. Also, another thing to point out, in Print_Array method, you are using size_col instead of size_row for the x_i loop, which looks like a logical error.
I've rewritten the constructor and Print_Array to look this way:
twoD_Array::twoD_Array(int size_col, int size_row)
{
this->size_col = size_col;
this->size_row = size_row;
Array = new int *[size_col];
for (int i = 0; i < size_col; i++)
{
Array[i] = new int[size_row];
for (int j = 0; j < size_row; j++)
{
Array[i][j] = 0;
}
}
}
void twoD_Array::Print_Array()
{
for (int y_i = 0; y_i < size_col; y_i++)
{
for (int x_i = 0; x_i < size_row; x_i++)
{
std::cout << Array[y_i][x_i];
}
std::cout << std::endl;
}
}
In
twoD_Array::twoD_Array(int size_col, int size_row)
int size_col defines a new temporary and local to the constructor variable named size_col. This size_col has nothing to do with the member variable size_col and because they have the same name the local variable hides the member variable.
End result is in Print_Array the member variable size_col has not been set to anything, so Crom only knows what will happen.
Solution:
Set the member variable:
twoD_Array::twoD_Array(int col, int row): size_col(col), size_row(row)
The colon (:) tells the compiler that a Member Initializer List is coming. The Member Initializer List allows you to initialize class member before entering the body of the constructor. This is very important when you have a member variable that has no default constructor or requires expensive initialization you don't want to be forced to do twice. It' also allows you to initialize a base class.
Note that I also changed the names of the parameters to prevent future confusion.
And while we are here, let's head off what is likely to be OP's next problem by asking What is The Rule of Three? Seriously. Click the link. Save you a ton of debugging.

C++ Dynamic Constructor

I'm getting the error:
no operator "[]" matches these operands
For the line:
cout << A[j].display(n)
But when I take out the [j], I'm getting the error:
class "list" has no member "display"
Here is my code:
class list
{
protected:
int *p; // pointer to a list
int size; // dimension
public:
list(int x)
{
size = x;
p = new int[size];
}
void get(int i, int val)
{
p[i] = val;
}
};
class dlist : public list
{
public:
int display(int i)
{
return p[i];
}
};
int main()
{
int n;
cout << "Enter elements in a row\n";
cin >> n;
list A(n);
int j, val;
for (j = 0; j < n; j++)
{
cin >> val;
A.get(j, val);
}
cout << "\n";
cout << "List of elements are :\n";
cout << "----------------------\n";
for (j = 0; j < n; j++)
cout << A[j].display(j) << "\t";
cout << "\n";
return 0;
}
Please help!
The class list indeed has no member function display. It is the class dlist declared like
class dlist : public list
//...
has such a member function. However the object A was defined in main like an object of class list
list A(n);
Moreover you can not create an object of the type dlist because you have to define explicitly a constructor of the type.
If you want to use polymorphism then introduce virtual functions making for example the member function display like virtual.
Also this syntax
cout << A[j].display(j) << "\t";
^^^^^^^^^^^^^^
is incorrect because the object A is a scalar object. It is not an array of objects and the class has no overloaded operator [].
You should start by defining explicitly a virtual destructor (otherwise there can be a memory leak in your program) that delete dynamically allocated memory in the constructor with parameter, copy constructor and the copy assignment operator in the class list. Then you can define the subscript operator and so on.

operator overloading in c++ - A query

I was reading a book on operator overloading in c++ and I encountered the following code:
class Array {
...
public:
Array & operator << ( int x) {
// insert x at the end of the array
}
};
Next It says: that overloading of the form a << x << y << z ;
wll not workSo It suggests that second invocation is treated as :( (a << x)<< y ) << z .so it recommends using return *this;
But I am not getting how return *this functions here? Please help!Here is the entire code:
#include <iostream>
#include <cstdlib>
using namespace std;
class Array {
int *a;
int capacity;
int size;
int incr;
public:
Array (int c=10) {
a = new int[c];
capacity = c;
for (int i=0; i<c; i++) a[i]=0;
size=0;
incr = c;
}
Array &operator << (int x) {
if(size<capacity) a[size++] = x;
else {
int *tmp = new int [capacity+incr];
for (int i=0; i<size; i++) tmp[i]=a[i];
delete[] a;
a = tmp;
a[size++]=x;
capacity = capacity+incr;
}
return *this;
};
int operator [] (int i) {
if(i<size) return a[i];
};
};
int main (int argc, char *argv[]) {
int s = atoi (argv[1]);
Array A (s);
for (int i=0; i<s; i++) A << i << i+1;
for (int i=0; i<s; i++) cout << A[i] << endl;
}
This really has nothing to do with operator overloading. It's called chaining and it's easier to explain using regular member functions. Suppose you defined a member function called insert like this:
Array& insert(int x) {
// insert x at the end of the array
return *this;
}
The return *this will return a reference to the current object so that you can chain calls like this:
Array a;
a.insert(0).insert(1).insert(2);
Which is essentially equivalent to:
Array a;
a.insert(0);
a.insert(1);
a.insert(2);
Each call to insert() will return a reference to the original object, allowing other calls to be made using that returned reference. You can overload the << operator to do the same thing:
Array& operator<<(int x) {
// insert x at the end of the array
return *this;
}
Now you can chain calls like this:
Array a;
a << 0 << 1 << 2;
You may be getting confused because of the spacing of Array &operator <<. The return value of the function is Array&, a reference to the array object.
Here's an example. In your call A << i << i+1, the A << i is called first and a reference to the updated A is returned. Next A << i+1 is called, with that new reference.
Yes everything is ok with your code. operator << in your semantics will and returning refference to same object which called it. You can see same in code of operator << of std::ostream and operator >> of std::istream.