Passing two dimensional array into function in C++ - c++

I'm trying to pass a two-dimensional array to the numOfStudents function, but Visual Studio is giving me an error saying:
no instance of overloaded "numOfStudents" matches the argument list
I've been trying everything and I can't find a solution.
#include <stdio.h>
#include <cstring>
//Prototypes
int unsigned numOfStudents(char **namesArray, int *, FILE *);
void printArrays(char **namesArray, int *marksArray, int loopCounter);
int unsigned minMark(int);
int unsigned maxMark(int);
int unsigned averageMark(int);
//Two constants used later to create array of characters
const int MAX_SIZE = 10;
const int NAME_LENGTH = 30;
int main()
{
// Declaring array which will hold all names from file
char namesArray[MAX_SIZE][NAME_LENGTH];
int unsigned studentNumber = 0;
int loopCounter = 0;
int marksArray[MAX_SIZE]; //Array will hold the marks of students, it needs to be same size as the previous array
FILE *file; //creating pointer to a file
file = fopen("names.txt", "r"); //telling the pointer where the file is and how to access it
loopCounter = numOfStudents(namesArray, marksArray, file);
//System pause - will hold console window open
getchar();
return 0;
}
int unsigned numOfStudents(int *marksArray, char **namesArray, FILE *file)
{
char tempArrayName[50]; //Temporary array to hold names
char tempArrayLastName[50]; //Temporary array to hold last names
int i = 0; //Counter
bool stop = false;
if(file == NULL)
{
printf("\nFile has been not opened correctly\n");
}
else
{
while (stop == false)
{
//Reading the file in order to get 3 separate lines which are assumed as a single record of one student
fscanf(file, "%s\n%s\n%d", tempArrayName, tempArrayLastName, &marksArray[i]);
//Following lines are compying strings from temp arrays into main array and adding a space for better readability
strcpy(namesArray[i], tempArrayName);
strcat(namesArray[i], " ");
strcat(namesArray[i], tempArrayLastName);
//Chcecking if the sentinel value was read in order to stop the loop
stop = strcmp(tempArrayName, "****");
i++; //adding the counter
//Checking if file is too big, before program will crash with an internal error
if (i == MAX_SIZE)
{
printf("ERROR!!! FILE TOO BIG TO READ!");
stop == true;
}
}
}
return i;
}

You declare numOfStudents with this argument list:
int unsigned numOfStudents(char **namesArray, int *, FILE *);
But then you define it with a different argument list:
int unsigned numOfStudents(int *marksArray, char **namesArray, FILE *file)
Note that your declaration has the char** as the first argument, but your definition has it as the second argument (and the opposite for the int*). The ordering of the arguments is very important and must match exactly, so you need to change your definition to match the declaration:
int unsigned numOfStudents(char **namesArray, int *marksArray, FILE *file)
{
....
}

2D arrays can be a pain to pass around, why dont you use std::string and pass an array (or reference to std::vector) of them around?

int unsigned numOfStudents(char **namesArray, int *, FILE *); // declaration
int unsigned numOfStudents(int *marksArray, char **namesArray, FILE *file) // definition
Check the order of the parameters in each. The definition's second param is actually declaration's 1st param. You should be good if you rectify the order

You have defined the signature of function numOfStudents as:
int unsigned numOfStudents(char **namesArray, int *, FILE *);
and you declaring the function as :
int unsigned numOfStudents(int *marksArray, char **namesArray, FILE *file)
{
---*** Your Code Here ***---
}
This is the problem that the "sequence of calling parameter" of the signature of the function and the function declaration should be same as the passing parameter. So it should be like :
int unsigned numOfStudents(char **namesArray, int *marksArray, FILE *file)
{
---*** Your Code Here ***---
}

Related

C Error:expected unqualified-id before '[' token

I want to write a program that can get the number of each different characters in a sentence.But when I use gcc to compile my code,it shows errors like this:
error::expected unqualified-id before '[' token. And these errors happen in these lines:
CountMachine[cnt].ch=*(S.ch);
CountMachine[cnt].count++;
if(*(S.ch)==CountMachine[j].ch)
.....
(where I use CountMachine[]).
Here is my complete code:
CountChar.h:
typedef struct
{
char ch;
int count=0;
}CountMachine[50];
typedef struct
{
char *ch;
int length;
}HString;
CountChar.cpp(but I use C's syntax)
void CountChar(HString S)
{
int cnt=0;
for(int i=0;i<S.length;i++)
{
if(i==0)
{
CountMachine[cnt].ch=*(S.ch);
CountMachine[cnt].count++;
cnt++;
S.ch++;
}
else
{
for(int j=0;j<cnt;j++)
{
if(*(S.ch)==CountMachine[j].ch)
{
CountMachine[j].count++;
S.ch++;
break;
}
if(j==cnt-1)
{
CountMachine[cnt].ch=*(S.ch);
CountMachine[cnt].count++;
cnt++;
S.ch++;
}
}
}
}
printf("There are %d different characters.\n",cnt-1);
for(int m=0;m<cnt-1;m++)
{
printf("the number of character %c is %d",CountMachine[m].ch,CountMachine[m].count);
}
}
You declare CountMachine as a type which includes 50 structures containing a character and an integer in CountChar.h and then you address the type itself in CountChar.cpp.
You cannot address a specific item in a type, you need to either create a variable of type CountMachine or remove the keyword typedef from the declaration of CountMachine in the header.
You have a very fancy type alias that declares a CountMachine type, not a variable that contains an array of 50 unnamed structs.
typedef struct
{
char ch;
int count=0;
}CountMachine[50];
// CountMachine is a type (array 50 of unnamed struct)
// step-by step declaration is much more clear:
struct machine
{
char ch;
int count=0;
};
typedef struct machine machine_t;
machine_t machines[50];
// machines is a variable that holds an array of 50 machine_t

Error parsing argv as char*

I want to read argv[1] as char* so this is what I do
using namespace std;
const char *myarg = NULL;
void Plots(char* file);
int main( int size_t argc, char* argv[] ) {
myarg = argv[1];
cout<<" this is a test "<<argv[1]<<endl;
Plots(argv);
}
void Plots(char* fileList){
cout<< argument passed correctly "<<endl;
}
}
However, when executing I get
Error: Function Plot() is not defined in current scope :0:
argv is a char *[], which for all practical purposes is char **.
You declared
void Plots(char * file);
The parameter to Plots() is a char *.
Plots(argv);
This attempts to pass a char ** to a function that takes char * as a parameter.
Additionally, although you declared
void Plots(char * file);
but then you went ahead and defined
void Plot(char* fileList)
Furthermore:
cout<< argument passed correctly "<<endl;
There's a quote missing here.
So, there's three different bugs here.

expected primary-expression before 'unsigned' in my program (destructors and constructors problems)

Hi I am new in c++ and I make skeleton of program and I have some problems with destructors and constructors.
My head.cpp:
#include <iostream>
#include "set_char.hpp"
using namespace std;
int main()
{
set_char *z1 = new set_char(unsigned char *zbior[]);
delete z1;
return 0;
};
My set_char.hpp class file:
#define ROZMIAR_MAX 256
class set_char
{
unsigned char zbior[ROZMIAR_MAX];
public:
set_char(unsigned char *zbior[]);
~set_char(unsigned char *zbior[]);
int nalezy(unsigned char);
int licznosc();
void dodaj(unsigned char);
void usun(unsigned char);
};
And my set_char.cpp file:
#include "set_char.hpp"
#include <iostream>
#include <math.h>
using namespace std;
set_char(unsigned char *zbior[]);
~set_char(unsigned char *zbior[]);
void set_char::dodaj(unsigned char)
{
};
void set_char::usun(unsigned char)
{
};
int set_char::nalezy(unsigned char)
{
};
int set_char::licznosc()
{
};
Among others:
you should not add any parameters in destructors:
~set_char(unsigned char *zbior[]);
^^^^^^^^^^^^^^^^^^^^^^ --- remove it
When creating set_char you should provide pointer to your array, and not the actual parameter type:
set_char *z1 = new set_char(unsigned char *zbior[]);
^^^^^^^^^^^^^^^^^^^^^^
1
You did not define your Constructor and Destructor
You declared them both in your set_char.hpp
set_char(unsigned char *zbior[]);
~set_char(unsigned char *zbior[]);
However in set_char.cpp you re-declare them again without a return type. Defining a Constructor and Destructor outside of a class is illegal. Your compiler thinks they are functions and searches for a return type.
2
a Destructor may not have any arguments.
3
If you define an array as an argument in a Function or a Constructor or Destructor with brackets '[]', it may not be of variable length, thus it must be defined. If it is intended to be of variable length, it must be left out.
4
You are calling the constructor in a bad way using:
new set_char(unsigned char *zbior[]);
You already declared what arguments it takes, so hand it the arguments. A null pointer, for example.
The correct way to do it in set_char.hpp:
set_char(unsigned char *);
~set_char();
The correct way to do it in set_char.cpp:
set_char::set_char(unsigned char *zbior)
{
//Your definition
}
set_char::~set_char();
{
//Your definition
}
The correct way to do it in head.cpp:
set_char *z1 = new set_char(0x0);
Also side-note, usually using the define macro to define constants, is a C way. In C++ it is usually done with:
static const size_t ROZMIAR_MAX 256;
Second side-note. It is considered 'neater' code if you have your constants/functions and whatnot defined inside a namespace

no matching function for call to while passing array

I am getting an error while writing a constructor which has some arguments to be passed. Searched previous threads but I am not getting it cleared.
Here is my code :
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int curr_state;
//int signal[3],trans_in[7],trans_fin[7],trans_sig[7];
class state_machine
{
private :
int start,end;
public :
state_machine(const int Q[],const int signals[],const int trans[][10][10],const int start,const int end);
// int mystate();
int run();
int get_signal( );
int get_state();
int change_state();
};
state_machine::state_machine(const int Q[],const int signals[],const int trans[][10][10],const int start,const int end)
{
}
int main(int argc, char * argv[])
{
state_machine s;
int Q[5]={1,2,3,4,5};
int signals[3]={0,1,2};
int trans[][3][1]={{1,0,2},{1,1,0},{1,2,0},{2,0,3},{2,1,0},{2,2,0},{3,0,5},{3,1,4},{3,2,2},{4,0,2},{4,1,0},{4,2,0},{5,0,0},{5,1,0},{5,2,0}};
int start=1,end=5;
//s.state_machine(Q,signals,trans,start,end);
}
Getting output as :
state_machine.cpp: In function ‘int main(int, char**)’:<br>
state_machine.cpp:27:16: error: no matching function for call to ‘state_machine::state_machine()’<br>
state_machine.cpp:21:1: note: candidates are: state_machine::state_machine(const int*, const int*, const int (*)[10][10], int, int)<br>
state_machine.cpp:8:1: note: state_machine::state_machine(const state_machine&)
The constructor for state_machine expects an int trans[][10][10]. This must be matched by the type of the trans array in main.
You must change your main to
int main(int argc, char * argv[])
{
int Q[5]={1,2,3,4,5};
int signals[3]={0,1,2};
int trans[][10][10]={{1,0,2},{1,1,0},{1,2,0},{2,0,3},{2,1,0},{2,2,0},{3,0,5},{3,1,4},{3,2,2},{4,0,2},{4,1,0},{4,2,0},{5,0,0},{5,1,0},{5,2,0}};
int start=1,end=5;
state_machine s(Q,signals,trans,start,end);
return 0;
}
Although the initializer list for trans doesn't fit the required type.
Your 3D arrays sizes that you specified needs to match exactly, which it does not. As the error says it is expecting const int (*)[10][10]
if int trans[][3][1] became int trans[][10][10] this error will go away
First of all you cannot create state_machine s; as no body for ctor w/o parameters are available.
Secondly, you need to call last line as state_machine s(Q,signals,trans,start,end);
Thirdly, 3rd parameter passing for ctor is incorrect. for 3d array, indexes should match. make int trans[][3][1] to int trans[][10][10] or vice-versa.

Access reading error when using class member variable

I have a class with private member variables declared in a header file. In my constructor, I pass in some filenames and create other objects using those names. This works fine. When I try to add another member variable, however, and initialize it in the constructor, I get an access reading violation. I sent the code to someone else and it works fine on his computer. Any idea what could be wrong?
Here is the offending code:
The .h file:
class QUERYMANAGER {
INDEXCACHE *cache;
URLTABLE *table;
SNIPPET *snip;
int* iquery[MAX_QUERY_LENGTH];
int* metapointers[MAX_QUERY_LENGTH];
int blockpointers[MAX_QUERY_LENGTH];
int docpositions[MAX_QUERY_LENGTH];
int numberdocs[MAX_QUERY_LENGTH];
int frequencies[MAX_QUERY_LENGTH];
int docarrays[MAX_QUERY_LENGTH][256];
int qsize;
public:
QUERYMANAGER();
QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname);
~QUERYMANAGER();
This is the .cpp file:
#include "querymanagernew.h"
#include "snippet.h"
using namespace std;
QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname){
cache = new INDEXCACHE(indexfname, btfname);
table = new URLTABLE(urltablefname);
snip = new SNIPPET(snippetfname, snippetbtfname);
//this is where the error occurs
qsize = 0;
}
I am totally at a loss as to what is causing this - any ideas?
Thanks, bsg
Suggestion, factor out the arrays:
class QUERYMANAGER
{
// Snip
int* iquery[MAX_QUERY_LENGTH];
int* metapointers[MAX_QUERY_LENGTH];
int blockpointers[MAX_QUERY_LENGTH];
int docpositions[MAX_QUERY_LENGTH];
int numberdocs[MAX_QUERY_LENGTH];
int frequencies[MAX_QUERY_LENGTH];
int docarrays[MAX_QUERY_LENGTH][256];
int qsize;
// Snip
};
Looks like you should have another structure:
struct Info
{
int* iquery;
int* metapointers;
int blockpointers;
int docpositions;
int numberdocs;
int frequencies;
int docarrays[256];
};
And the QueryManager now looks like:
class QueryManager
{
INDEXCACHE *cache;
URLTABLE *table;
SNIPPET *snip;
int qsize;
Info details[MAX_QUERY_LENGTH];
};
This may help encapsulate themes a little better.
Your dependencies are probably not right, and the necessary files aren't getting rebuilt. Try a "clean" rebuild.
As a note to style, use initializer lists.
QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname,
char *snippetfname, char *snippetbtfname) :
cache(new INDEXCACHE(indexfname, btfname)),
table(new URLTABLE(urltablefname)),
snip(new SNIPPET(snippetfname, snippetbtfname)),
qsize(0)
{
}
and you may not need to make those items pointers:
class QUERYMANAGER {
INDEXCACHE cache;
URLTABLE table;
SNIPPET snip;
...
QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname,
char *snippetfname, char *snippetbtfname) :
cache(indexfname, btfname),
table(urltablefname),
snip(snippetfname, snippetbtfname),
qsize(0)
{
}
Have you built clean? Since accessing the last member variable blows up, but assigning to earlier ones works OK, either you're not constructing/allocating the instance right when you do use it, or you have object files that refer to older versions of the header that didn't have qsize in the object yet, and thus aren't allocating enough space. Or something along those lines.
As expected, this runs just fine on my machine:
#include <cstdlib>
struct INDEXCACHE {};
struct URLTABLE {};
struct SNIPPET {};
const std::size_t MAX_QUERY_LENGTH = 256;
class QUERYMANAGER {
INDEXCACHE *cache;
URLTABLE *table;
SNIPPET *snip;
int* iquery[MAX_QUERY_LENGTH];
int* metapointers[MAX_QUERY_LENGTH];
int blockpointers[MAX_QUERY_LENGTH];
int docpositions[MAX_QUERY_LENGTH];
int numberdocs[MAX_QUERY_LENGTH];
int frequencies[MAX_QUERY_LENGTH];
int docarrays[MAX_QUERY_LENGTH][256];
int qsize;
public:
QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname);
};
QUERYMANAGER::QUERYMANAGER(char *indexfname, char *btfname, char *urltablefname, char *snippetfname, char *snippetbtfname)
: cache(new INDEXCACHE(/*indexfname, btfname*/))
, table(new URLTABLE(/*urltablefname*/))
, snip(new SNIPPET(/*snippetfname, snippetbtfname*/))
, qsize(0)
{
}
int main()
{
QUERYMANAGER foo("blargl", "frxnl", "wrgxl", "brlgl", "srgl");
return 0;
}
So the error must be in the code you're not showing.
BTW, all upper-case names are boo except for macros. They're making your code harder to read and confuse everyone used to a more common coding style.