g++ compiler is not recognizing my function - c++

I'm just starting to code, and am learning about arrays right now. I am trying to write a program that takes in a list of arrays, and tells me if the first or last number is a 2. To do this, I'm using a function.
My code looks like:
#include <iostream>
using namespace std;
const int size = 6;
bool firstlast(int array[size]);
int main()
{
int array[size];
for (int index = 0; index < size; index++)
{
cout << "Enter value for array[" << index << "]\n";
cin >> array[index];
}
bool check = firstlast(array[size]);
if (check)
cout << "The array either starts or ends in 2!\n";
else
cout << "The array does not start or end with 2.\n";
return 0;
}
bool firstlast(int array[size])
{
if (array[0] == 2)
return true;
if (array[size - 1] == 2)
return true;
return false;
}
What am I doing wrong?
The compiler gives me the error:
candidate function not viable: no known conversion from 'int' to 'int *' for 1st argument; take the address of the argument with and

The compiler is recognising your function fine.
The problem is in the manner your code calls the function
bool check = firstlast(array[size]);
which attempts to pass array[size] (a non-existent element of array) to a function expecting a pointer.
The call, presumably, should be
bool check = firstlast(array);
since arrays are implicitly converted to pointers when passed to functions.

This code
bool check = firstlast(array[size], size);
tries to pass the sizeth element of array not the array itself. In C++ arrays are passed by pointer, even if you write the function parameter with array syntax.
To avoid confusing yourself, change firstlast to
bool firstlast`(int* array, int size)`
and call it with
bool check = firstlast(array, size);

Related

Data don't insert into an array of pointers

I have my class CompressionAlgorithm from which classes RLE and MTF inherits. I made an array to which I am trying to add these child classes but only first class gets added.
int const size = 2;
CompressionAlgorithm * CA[size];
CA[0] = new RLE();
CA[1] = new MTF();
Both RLE and MTF get initialized but when I am trying to print their name using printall method MTF doesn't get any info printed on console or I am getting an error saying std::bad_alloc.
Print p;
p.printall(*CA, (size));
void Print::printall(CompressionAlgorithm *ca, int size)
{
for (int i = 0; i < size; i++)
{
cout << i+1 << " for ";
cout << ca[i].GetName();
cout << "\n";
}
}
Where am I making a mistake?
You need make function to accept array of pointers not just a pointer:
void Print::printall(CompressionAlgorithm *ca[], int size)
and whil calling you need to call like this:
p.printall(CA, (size));
You don't need to dereference the pointer when passing it to the function; doing so only passes the first element. Additionally the function signature needs to accept an array of pointers, which is what you have.
instead of calling *ca in the printall() call only ca as calling *ca only accesses the first element of the array when not in a loop.

Getting "vector subscript out of range" error

I'm currently trying to get "variadic functions" down and just trying to load 4 names in a string vector and then print them out. When I do this with 'int' type and use numbers, it works fine, but when I use a string vector I get the error.
#include "stdafx.h"
#include<cstdio>
#include<cstdarg>
#include<string>
#include<vector>
#include<iostream>
using namespace std;
int count;
vector<string> namesVector;
void names(int count, ...)
{
va_list namesList;
int i; // for loop
va_start(namesList, count);
for (i = 0; i < count; i++)
{
string currentElement;
currentElement = va_arg(namesList, string);
namesVector[i] = currentElement;
}
va_end(namesList);
}
int main()
{
int nameCount = 4;
names(nameCount,"jon", "maggie", "joan", "alfred");
for (int i = 0; i < nameCount; i++)
{
cout << "Name at element " << i << " is: " << namesVector[i] << endl;
}
}
C++ only allows to use trivially-copyable types as variadic arguments. As std::string is constructible from a char* pointer which points to a null-terminated buffer, you may use char* instead of std::string type. Just replace
currentElement = va_arg(namesList, string);
with
currentElement = va_arg(namesList, char*);
in your code. To get rid of this limitation, consider variadic templates, which generate code in compile-time for any type you use.
Your code also contains a run-time error. This:
namesVector[i] = currentElement;
is very likely to crash your program as you didn't allocate any memory in the vector. Vectors are actually dynamic arrays, so you should either pass a size argument to appropriate constructor, or call resize on the vector. In you case you may do neither of it, but just use push_back method:
namesVector.push_back(currentElement);

Initializing chained hash table to NULL. Get "lvalue required as left operand of assignment" error. Why? Here is my code:

I am trying to create a chained hash table. I have started by building a dynamic array and am now trying to initialize each array pointer to NULL. But I get the error "lvalue required as left operand of assignment". Why? Here is my code:
#include <iostream> // for i/o functions
using namespace std;
const int HTSIZE = 997; //size of the hash table
struct CHTNode
{
int value;
CHTNode *next;
};
void InitializeTable(CHTNode* &cHT);
int main()
{
CHTNode *chainedHT;
chainedHT = new(nothrow) CHTNode[HTSIZE];
if (chainedHT == NULL)
{
cout << "ERROR: Memory allocation error"
<< endl;
return 1;
} //end if
else
{
InitializeTable(chainedHT);
}
}
void InitializeTable(CHTNode* &cHT)
{
for (int i = 0; i < HTSIZE; i++)
&cHT[i] = NULL; //ERROR FOR THIS LINE
}
The address-of operator & returns the address of the given expression, so &cHT[i] evaluates to the address of the ith element of cHT. It seems you're trying to assign to the variable cHT[i], but what you're doing right now is trying to assign to the address value of cHT[i], which makes no more sense than trying to assign to a constant.
You do not have an array of pointers. You allocated an array of objects of type CHTNode.
You could value initialize this array when it was allocated. For example
chainedHT = new(nothrow) CHTNode[HTSIZE] {};
If you want to write a separate function that zero-injitialize each element of the array then the function could be declared like
void InitializeTable( CHTNode* cHT, int n );
and defined like
void InitializeTable( CHTNode* cHT, int n )
{
for ( int i = 0; i < n; i++ ) cHT[i] = {};
}
As for erroneous statement
&cHT[i] = NULL;
then it does not make a sense. Expression &cHT[i] is a temporary object that you are try to assign.

bool find function did not search in array

I want to make find function for my class that searches an array of items and return 1 if it is found and return 0 otherwise.
Here my code but always returns 0:
// implementation file
bool bag::find(string item_name)
{
for (int i = 0; i < 5; i++)
if (arr[i] == item_name)
return true;
return false;
}
string arr[5]={"a","b","c","d","e"}
max size of array is 5
// main
cout << find("a");
How I can make it search correctly?
Just use std::find:
bool bag::find(std::string item)
{
return std::find(arr, arr + 5, item) != &arr[5];
}
I'm assuming arr is a C-style array until you say otherwise.
If you defined your array as having element type char then indeed your function will not work because an object of type char can not be implicitly converted to type std::string.

cannot convert from 'std::string' to 'char'

Changed completely due to suggestions from other member. Most problems solved, still having problems. Now won't output any names from the array in main. Not sure if I'm passing them back correctly from function.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void bubblesort(string[], const int);
int sub = 0;
int main()
{
const int maxsize = 100;
string friendArray[maxsize];
ifstream friends;
friends.open("myFriends.dat");
while (sub < maxsize)
{
getline(friends, friendArray[sub]);
sub++;
}
bubblesort(friendArray, maxsize);
cout<<friendArray[0]<<" "<<friendArray[1]<<" "<<friendArray[2];
system("pause");
return 0;
}
void bubblesort(string *array, const int size)
{
bool swap;
string temp;
do
{
swap = false;
for (int count = 1; count < (size - 1); count++)
{
if(array[count-1] >array[count])
{
temp = array[count-1];
array[count-1] = array[count];
array[count] = temp;
swap = true;
}
}
}
while(swap);
}
Your problem isn't necessarily that temp inside bubblesort is not a char, the problem is that array is declared as a string and not a string[].
The reason you're getting the error is because array[count+1] is of type char, and temp is of type string. std::swap expects two elements of the same type.
However, that may be the least of your problems, your code doesn't compile for quite a few reasons. Not just that but you're passing in maxsize to bubblesort at each iteration. There's a flaw in both your logic and your syntax.
EDIT: Since you're still having trouble getting the sorting to work, here's a working modification of your code:
#include <iostream>
void bubblesort(std::string array[], size_t size)
{
bool bSwapped;
std::string temp;
do
{
bSwapped = false;
for (size_t count = 1; count < size; count++)
{
if(array[count-1] > array[count])
{
std::swap(array[count-1], array[count]);
bSwapped = true;
}
}
}
while(bSwapped);
}
int main(void)
{
std::string array[] = { "def", "ghk", "abc", "world", "hello" };
bubblesort(array, sizeof(array)/sizeof(*array));
for (size_t i = 0; i < sizeof(array)/sizeof(*array); ++i)
std::cout << array[i] + " ";
std::cout << std::endl;
return 0;
}
bubblesort could also be written as: void bubblesort(std::string *array, size_t size). There's no difference in this case since, when passed to a function, arrays decay into pointers.
Since arrays are passed by reference, a pointer to the first element, any modifications made to array inside of bubblesort will actually be modifying your array in main. So that's how arrays are "returned".
std::vector is a good alternative to the standard array, since it automatically resizes and obviously contains the length of the internal array so that you don't have to pass the size everywhere you pass an std::vector. You can also use it the same way as a regular array.
temp is a string, array[count] is a char (since an std::string is a vector of char elements.) I'm not sure what you're trying to do here, but the compiler is correct - you can't assign a char to a string.
You could change temp to be a char, since all you do with it is assign a char to it, and then assign it back to an element of array, which is also a char.
You need to declare temp as char. You can use std::swap to avoid such mistakes in the future:
std::swap(array[count], array[count+1]);
This would make your code compile, but it would not do what you're trying to do (bubblesort). The problem is that you are passing a single string (which is also an "array" of characters) instead of an array of strings, which is, in a very lose sense, "an array of arrays of characters". Your bubblesort needs to accept string *array as its first parameter.