unqualified-id before '[' token c++ error - c++

I'm doing a c++ app and I'm having some troubles while doing it and I can't fix the error.
I have a class ratinmaze that I'd like to use in the main class, but i get a lot of errors..
ratinmaze.h
#ifndef RATINMAZE_H
#define RATINMAZE_H
#include <iostream>
class ratinmaze
{
public:
ratinmaze();
int [5][5] solution; //error
virtual ~ratinmaze();
ratinmaze(int N);
void solveMaze(int [][5] maze, int N);
bool findPath(int [][5] maze, int x, int y, int N, std::string direction);
bool isSafeToGo(int [][5] maze, int x, int y, int N);
void print(int [][5] solution, int N);
void print(int [][5] solution, int N);
protected:
private:
std::string direction;
int N;
int y;
int x;
};
#endif // RATINMAZE_H
main.cpp
#include <iostream>
#include "ratinmaze.h"
using namespace std;
int main()
{
int N = 5;
int[5][5] maze = {{ 1, 0, 1, 1,1 },
{ 1, 1, 1, 0,1 },
{ 0, 0,0, 1, 1 },
{ 0, 0, 0, 1,0 },
{ 0, 0,0, 1, 1 }};
ratinmaze r = new ratinmaze(N);
r.solveMaze(maze, N);
return 0;
}
I get the error in the first line :
expected unqualifie-id before '[' token and in the following i get
"error : expected ',' or '...' before 'maze'
If u guys could help me I would be really appreciated!
#update
I figured out that I was not creating the right way my arrays but I have an error here on my main : ratinmaze r = new ratinmaze(N);
the error is :
invalid conversion from 'ratinmaze*' to 'int' [-fpermissive]
Any idea ?

Arrays in C++ are declared with the square brackets after the variable name, like so:
int solution[5][5];

Related

Error C2280 : Class::Class(void) : Attempting to reference a deleted function

So, I am working on a project, and I have two files in this project:
main.cpp, matrix.h
The problem is that My code seemed to work perfectly a few hours ago, and now it doesn't
main.cpp:
#include <iostream>
#include "matrix.h"
#include <vector>
int main() {
Matrix f;
f.create(10, 1, {3, 4, 5, 6, 7, 8, 9});
}
matrix.h:
#pragma once
#include <iostream>
#include <string>
#include <Windows.h>
#include <vector>
class Matrix {
public:
const size_t N;
bool ifMatrixCreated;
const char* NOTENOUGH = "The size of the array should match to the width*height elements";
std::vector<int> arr;
int w, h;
void create(int width, int height, const std::vector<int> a) {
w = width;
h = height;
if (a.size() != width * height) {
ifMatrixCreated = false;
std::cout << "bello";
}
else {
ifMatrixCreated = true;
arr = a;
std::cout << "hello";
}
}
};
And when I compile, it generates this error (Using VS2019):
Error C2280 | 'Matrix::Matrix(void)': attempting to reference a deleted function Matrix | Line 5
It keeps saying that "The default constructor of Matrix cannot be referenced - It is a deleted function"
Can you help solve this error?
Thanks in advance.
Here is the correct working example. The error happens because every const data member must be initialized. And
The implicitly-declared or defaulted default constructor for class T is undefined (until C++11)defined as deleted (since C++11) if any of the following is true:
T has a const member without user-defined default constructor or a brace-or-equal initializer (since C++11).
#pragma once
#include <iostream>
#include <string>
//#include <Windows.h>
#include <vector>
class Matrix {
public:
//const size_t N;//this const data member must be initialised
const size_t N = 6;
bool ifMatrixCreated;
const char* NOTENOUGH = "The size of the array should match to the width*height elements";
std::vector<int> arr;
int w, h;
void create(int width, int height, const std::vector<int> a) {
w = width;
h = height;
if (a.size() != width * height) {
ifMatrixCreated = false;
std::cout << "bello";
}
else {
ifMatrixCreated = true;
arr = a;
std::cout << "hello";
}
}
};

why am i getting error for parameters of class function? "no matching function for call" C++

error: no matching function for call to 'Player::setX(int&, bool&)'
in main:
//player
pc.setX(pc.x, *actions); //actions is an arr of input possibilities.
pc.setY(pc.y, *actions);
in player.h:
public:
int x = 1, y = 1;
int setY (int y, int *actions);
int setX (int x, int *actions);
secondary question: is it possible to pass x/y as a struct instead of individually?
all the code that should be necessary afaik:
bool actions[10]; //"up", "down", "left", "right", "skill1", "skill2", "skill3", "skill4", "skill5", "interact",
Player pc; //object creation
//player
pc.setX(pc.x, *actions);
pc.setY(pc.y, *actions);
#ifndef PLAYER_H
#define PLAYER_H
class Player
{
public:
int x = 1, y = 1;
int setY (int y, int *actions);
int setX (int x, int *actions);
private:
};
#endif //
Yes you can pass both variable in a struct as:
typedef struct { int x, int y}sXYInfo;
class Player
{ public:
sXYInfo m_sxy;
int setXY (sXYInfo sxy , int *actions);
};
and when calling use below, as you are passing array as a pointer:
pc.setXY(pc.m_sxy, actions);

Cannot initialize a parameter of type 'int *' with an rvalue of type 'int (*)[20]'

Main.cpp
#include "SelectionSort.h"
using namespace std;
int main() {
SelectionSort<int> sorterInt;
int test_array[20];
sorterInt.stuffNum(&test_array, 20, 1, 200);
}
SelectionSort.h
using namespace std;
template <typename T>
class SelectionSort {
public:
void stuffNum(T *object, int size, int min, int max)
{
for(int i = 0; i < size; i++)
{
(*object)[i] = 5;
}
}
SelectionSort<int> sorterInt;
int test_array[20];
sorterInt.stuffNum(&test_array, 20, 1, 200);
your template have type int so your method take a int * as argument.
And you write &test_array who has type int *[20] because you send the address of your array.
So just remove the &
sorterInt.stuffNum(test_array, 20, 1, 200);
You need a better understanding of pointer.
edit: (read comment)
(*object)[i] = 5;
here you should remove * and () like this
object[i] = 5;
more doc here What is array decaying?

C++ Error C2061: syntax error : identifier

When I try to compile this code I get these Errors:
Error 1 error C2061: syntax error : identifier 'stammdaten'
Error 2 error C2660: 'Test_Lohnab::Gehaltsrechner' : function does not take 1 arguments
Error 3 error C2511: 'int Test_Lohnab::Gehaltsrechner(stammdaten &)' : overloaded member function not found in 'Test_Lohnab'
It would be a big help, if someone could explain me what i did wrong.
This is my code:
//Test_Lohnab.h
#pragma once
#include <iostream>
#include <string>
class Test_Lohnab
{
public:
Test_Lohnab();
~Test_Lohnab();
int Gehaltsrechner(stammdaten &st);//, abrechnung &ab);
}
class stammdaten
{
public:
std::string name;
std::string strasse;
std::string ort;
….
public:
stammdaten();
~stammdaten();
stammdaten(std::string n, std::string s, std::string o, int st, int fa, int k, int a, double z, double kver, double pk, int mi, int pv, int os, int ps, int ki, double geb,
double jf, double jh, int G_c, int G_a_c, int r_c, int A_L_c, double U_1, double U_2, double bb_g);
}
//Test_Lohnab.cpp
#include "Test_Lohnab.h"
#include <iostream>
Test_Lohnab::Test_Lohnab()
{
}
Test_Lohnab::~Test_Lohnab()
{
}
stammdaten::stammdaten()
{
}
stammdaten::stammdaten(std::string n, std::string s, std::string o, int st, int fa, int k, int a, double z, double kver, double pk, int mi, int pv, int os, int ps, int ki, double geb,
double jf, double jh, int G_c, int G_a_c, int r_c, int A_L_c, double U_1, double U_2, double bb_g)
: name(n), strasse(s), ort(o), stkl(st), f(fa), krv(k), abv(a), zkf(z), kv(kver), pkpv(pk), mitag(mi), pvz(pv), ost(os), pvs(ps), kist(ki), gebjahr(geb), jfreib(jf), jhinzu(jh),
Gleitzone_check(G_c), Gleitzone_alt_check(G_a_c), rente_check(r_c), AN_Lst_check(A_L_c), U1(U_1), U2(U_2), bbg(bb_g)
{
}
int main()
{
system("PAUSE");
stammdaten Personstamm1{ "Klaus Müller", "Parkstrasse 12", "78578 Monopoly", 1, 1000, 1, 1, 0, 15.5, 0.0, 0, 1, 0, 0, 9, 1982, 0, 0, 0, 0, 0, 0, 1.7, 0.38, 0 };
/**/
Test_Lohnab Ablauf;
Ablauf.Gehaltsrechner(Personstamm1); //, Abrechnung1);
system("PAUSE");
/**/
}
int Test_Lohnab::Gehaltsrechner(stammdaten &Test1)//, abrechnung &Abrech1)
{
}
Your classes Test_Lohnab function int Gehaltsrechner needs an object of the type stammdaten which the compiler does not yet know about because it is defined afterwards. You can use forward declaration to inform him about it. Simply add the following line after your includes
class stammdaten;

Getting and setting a three dimensional array

I have to get a three dimensional array c[] w[] h[] from a class and convert it into a single dimensional array of unsigned char[]. I tried this way. But it doesnt work !!.. When I give the input through command line the execution halts and breaks...
Implementation:
#include <iostream>
#include<fstream>
#include<stdlib.h>
#include<vector>
//#include "E:\Marvin_To_UnsignedChar\MetisImg.hpp"
//#include "C:\Users\padmanab\Documents\Visual Studio 2013\Projects\Marvin_To_UnsignedChar\MetisImg.hpp"
extern "C"
{
#include "C:\Users\padmanab\Desktop\Marvin_To_UnsignedChar\multiplyImage\multiplyImage.h"
//#include "C:\Users\padmanab\Documents\Visual Studio 2013\Projects\Marvin_To_UnsignedChar\multiplyImage\multiplyImage.h"
}
using namespace std;
class Marvin_To_UnsignedChar
{
public:
int Color;
int Width;
int Height;
std::vector<unsigned char> values;
Marvin_To_UnsignedChar(int c, int w, int h) : Color(c), Width(w), Height(h), values(c*w*h){}
unsigned char operator()(int color, int width, int height) const
{
return values[Height*Width*color + Height*width + height];
}
unsigned char& operator()(int color, int width, int height)
{
return values[Height*Width*color + Height*width + height];
}
};
In Main():
int color; int width; int height;
std::cout << "Please enter the color value";
std::cin >> color;
std::cout << "Please enter the width value";
std::cin >> width;
std::cout << "Please enter the height value";
std::cin >> height;
Marvin_To_UnsignedChar M_To_V(color,width,height);
unsigned char test = M_To_V(color, width, height);
std::cout << test << '\n';
It would be great to have some guidance about the issue or may be a better method to implement it !
The class code is fine. The problem is that in
unsigned char test = M_To_V(color, width, height);
you are invoking the operator() with the dimensions as parameters (remember that you used before color, width, height to construct the Marvin_To_UnsignedChar object), so it will effectively output values[Color*Width*Height], which is one element past the end of the vector. Otherwise the code is fine, you probably want to use something like
unsigned char test = M_To_V(x, y, z);
where x, y, z are such that x<color, y<width, z<height.
For example, the code below works (I am initializing in the constructor the array with 'x' so you can see that something is printed out)
#include <iostream>
#include <vector>
using namespace std;
class Marvin_To_UnsignedChar
{
public:
int Color;
int Width;
int Height;
std::vector<unsigned char> values;
Marvin_To_UnsignedChar(int c, int w, int h) :
Color(c), Width(w), Height(h), values(c*w*h,'x'){}
unsigned char operator()(int color, int width, int height) const
{
return values.at(Height*Width*color + Height*width + height);
}
unsigned char& operator()(int color, int width, int height)
{
return values.at(Height*Width*color + Height*width + height);
}
};
int main()
{
int color = 3, width = 777, height = 600;
Marvin_To_UnsignedChar M_To_V(color,width,height);
M_To_V(2, 776, 599)='a'; // set the last element to `a`
std::cout << M_To_V(2, 776, 599) << '\n';
unsigned char test = M_To_V(1, 200, 300); // this element is pre-initialized with 'x'
std::cout << test << '\n';
}
PS: you can use values.at(position) instead of values[position] for a std::vector, and the former will check for out of bounds, i.e. throw an exception if you get an out of bound, so you can figure out what's going on. The values[position] form is faster, but I recommend using at at least in debug mode if you are not 100% sure that you may overflow.