Constructor errors - c++

I have this class header
//header for class.
#ifndef Container_H
#define Container_H
#include <iostream>
using namespace std;
const int DEFAULT=32;
class Container{
public:
Container(int maxCapacity = DEFAULT);
~Container();
void insert(int item, int index);
void erase(int index);
int size()const;
private:
int sizeC;
int capacityC;
int * elements;
};
void info();
#endif
and this source file
#include "container.h"
Container::Container(int maxCapacity = DEFAULT){
int y;
}
void Container::insert(int item, int index){
int x;
}
and when I compile this, I get the following error message
test.cpp:4: error: default argument given for parameter 1 of `Container::Container(int)'
container.h:12: error: after previous specification in `Container::Container(int)
what have I done wrong here?

Functions with no arguments still need the parentheses:
Container::Container() {
int y;
}
Based on your header, it looks like you just forgot the maxCapacity argument, and it should actually be:
Container::Container(int maxCapacity) {
int y;
}
(If you're asking about the warning too, it's pretty self-evident -- you declared an int x but didn't do anything with it)
EDIT: Well now you've edited it to completely change the error. Now it's an error because you're specifying the default argument in both places; you're only supposed to specify it in the declaration. Leave it out in the actual implementation, like my second example above

Your Container constructor (in the source file) should be like this:
Container::Container(int maxCapacity){
// code
}

Container::Container{
int y;
}
I'm not sure what this is intended to be. If you're trying to define your ctor, it should look something like:
Container::Container(int maxCapacity) // ....
Note that you want to include the default value in the declaration, but not in the definition.

Container::Container{
int y;
} is syntactically incorrect.
EDIT:
Try this:
Container::Container(int maxCapacity) // default argument not to be mentioned in the definition
{
int y;
}

Related

Not all of my constructors are being imported?

I'm making a heap class to be importable with heap.h and my constructors including bool types do not work, yet every other constructor and function imported works.
Here is what's in heap.h:
#ifndef __HEAP_INCLUDED__
#define __HEAP_INCLUDED__
#include <iostream>
#include <vector>
using namespace std;
class heap{
int capacity;
bool isMinHeap; //1 is min heap -- ascending order
vector<int> * content;
public:
heap();
heap(bool t);
heap(vector<int> * input);
heap(vector<int> * input, bool t);
void print();
void prettyPrint();
int parent(int i);
int leftChild(int i);
int rightChild(int i);
int size();
int getMax();
void insert(int data);
void heapifyDown(int index);
void heapifyUp(int index);
int invalidChild(int index);
int deleteMax();
int deleteMin();
bool minDir();
int at(int index);
};
vector<int> * heapSort(vector<int> * input);
void swap(vector<int> * vec, int a, int b);
#endif
Here are the defined constructors in heap.cpp. Note, all constructors work fine when I add a main to this file to test stuff:
class heap{
vector<int> * content;
int capacity = 256;
bool isMinHeap; //1 is min heap -- ascending order
public:
heap(){
content = new vector<int>;
isMinHeap = 0;
}
heap(bool t){
content = new vector<int>;
isMinHeap = t;
}
heap(vector<int> * input){
content = input;
isMinHeap = true;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
heap(vector<int> * input, bool t){
content = input;
isMinHeap = t;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
//other functions below
}
The constructors with bool do not work in main.cpp, which has #include "heap.h" at the top. The files are all in the same directory and I am compiling with this command: g++ heap.cpp main.cpp -o main. Why do two of my constructors not work?
The error I see is
/usr/bin/ld: /tmp/ccwomODk.o: in function `main':
main.cpp:(.text+0x4e2): undefined reference to `heap::heap(bool)'
collect2: error: ld returned 1 exit status
-Wall does not elaborate on the issue. I'm pretty sure the issue is with my linking somewhere because the constructors work inside of heap.cpp when I use them in there.
What you are doing with the class in the .cpp file is wrong. You are not allowed to define the class twice. There must only be one class heap { /*...*/ }; in the program (but it may be included in multiple .cpp files). Otherwise the one-definition-rule (ODR) is violated and the program has undefined behavior.
So remove everything you are showing from heap.cpp.
To define the constructors of heap in the heap.cpp file, you need to use this syntax:
#include "heap.h"
heap::heap() {
/*...*/
}
heap::heap(bool t) {
/*...*/
}
//...
and so on. The other member functions must be defined in a similar way, e.g.:
void heap::print() {
/*...*/
}
Furthermore, if you want to have a default member initializer as in
int capacity = 256;
add it in the declaration in the .h file instead.
I also want to add that having a pointer-to-std::vector as member is almost surely a wrong approach as well, but out-of-scope for the question.
When you declare a program element such as a class, function, or
variable, its name can only be "seen" and used in certain parts of
your program. The context in which a name is visible is called its
scope. For example, if you declare a variable x within a function, x
is only visible within that function body.
It seems you broke ODR rule so bad. Your class members including constructors has no body declared in the source file(heap.cpp).
Use '::' to make class members have a body:
//heap.cpp
"heap.h"
heap::heap()
{
}
heap:heap(vector<int> * input, bool t)
{
}
int heap::parent(int i)
{
return i;
}
// this is how you create a body for function that are class members
// the same should be done for all other functions

undefined reference to my constructor

I have a simple class which I cannot instantiate and I don't know why...
Please help me !
-------Test.cpp-------
#include<iostream>
using namespace std;
#include "meteo.h"
int main()
{
Meteo meteo;
}
-------meteo.h---------
#ifndef METEO_H
#define METEO_H
class Meteo
{
public:
Meteo();
int Get(int i);
private:
char *list[];
};
#endif
-------meteo.cpp--------
#include "meteo.h"
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
int Meteo::Get(int i)
{
return list[i];
}
I get the error: "undefined reference to `Meteo::Meteo()'"
It seems that the problem is that the compiler issued an error when was compiling the constructor
Meteo::Meteo()
{
list[]("Sec","Venteux","Humide");
}
and did not generate the object module.
This record
list[]("Sec","Venteux","Humide");
is invalid.
Try to change the class definition like
class Meteo
{
public:
Meteo();
int Get(int i);
private:
const char *list[3];
};
and define the constructor like
Meteo::Meteo() : list { "Sec","Venteux","Humide" }
{
}
The other reason might be that you did not include object module meteo in the project.
Take into account that this member function
int Meteo::Get(int i)
{
return list[i];
}
is also wrong. The type of elements of the array is const char * not int.

Issue with forward declaring class

So I am trying to forward declare a class in my C++ project and then create it in main.
So I have player_obj.cpp which contains the class, classes.h which forward declares the class, and main.cpp which uses it.
classes.h
#ifndef CLASSES_H
#define CLASSES_H
class player_class
{
public:
int x;
int y;
char sprite;
int xprevious;
int yprevious;
private:
bool active;
public:
void update_xy();
player_class(int _x, int _y, char _sprite);
void step();
void destroy();
};
#endif
main.cpp
#include <iostream>
#include "classes.h"
using namespace std;
int main()
{
player_class player_obj (5,5,'#');
cout << player_obj.x << ", " << player_obj.y << endl;
return 0;
}
and player_obj.cpp
#include <iostream>
#include <Windows.h>
using namespace std;
class player_class
{
public:
//Coordinates
int x;
int y;
//Sprite
char sprite;
//Previous coordinates
int xprevious;
int yprevious;
//Not everyone can set the activity
private:
//Active
bool active;
//Update xprevious and yprevious - Called by the step event
void update_xy()
{
xprevious = x;
yprevious = y;
}
//All functions public
public:
//Create event/Constructer
player_class(int _x, int _y, char _sprite)
{
//Set default variables
x = _x;
y = _y;
sprite = _sprite;
xprevious = x;
yprevious = y;
active = true;
}
//Step event
void step()
{
//Update old xprevious and yprevious
update_xy();
//Do other stuff here
}
//Drestroy event
void destroy()
{
active = false;
}
};
I thought that would work out all right but when I compile and run it I get:
main.cpp:(.text+0x2c): undefined reference to`player_class::player_class(int, int, char)'
I've done some research, but I can't seem to fix this issue.
I greatly appreciate any help!
Well you're sort of close, what you have in your header is indeed a class declaration (not a forward declaration mind you).
The problem is you never defined it. What you have in player_obj.cpp is an abomination of class redefinition, but you already have your class declared. Just include the header file and define the functions one by one and you're done!
#include "classes.h"
player_class::player_class(int _x, int _y, char _sprite)
{
//Set default variables
x = _x;
y = _y;
sprite = _sprite;
xprevious = x;
yprevious = y;
active = true;
}
// and so on
If you're serious about learning modern C++ though, a few notes:
#pragma once is the modern way of guarding header files. Don't use those #ifdef..#endif constructs.
generally speaking, don't name anything starting with underscores. Especially not parameters visible as part of your public contract.
you have class initializers for a reason, use them! You don't need half a screen of copy pasting variables in your constructors.
You dont want a forward declaration. You want a declaration. It is a classical case of declaring a class in a header file and defining its functions in a cpp file. Then including the header where-ever you want to use your class
You only need forward declarations when you want to use a pointer to that class as a parameter to a function or a member variable somewhere but the definition of that class is not available yet.
Note that when you forward declare a class, you cannot use this class's member variables or functions in that header
-regards
Gautam

Can't call a static method in Qt

I have a simple class containing a static attribute. There are two static methods in this class: one to get the static attribute and the other to initialize it. Yet when call the static method the compiler reports an error.
The class:
class Sudoku {
Cell Grid[9][9];
int CurrentLine;
int CurrentColumn;
void deleteValInColumn(int val, int col);
void deleteValInRow(int val, int row);
void deleteValInBox(int val, int x, int y);
static int unsetted; //!
public:
static void IniUnsetted() { //!
unsetted = 0;
}
static int GetUns() { //!
return unsetted;
}
Sudoku(ini InitGrid[9][9]);
void Calculate_Prob_Values();
Cell getCell(int x, int y);
QVector<int> getPossibleValues(int x, int y);
bool SolveIt();
};
This is the error I get:
In member function 'bool Sudoku::SolveIt()':
no return statement in function returning non-void [-Wreturn-type]
In function `ZN6Sudoku6GetUnsEv':
undefined reference to `Sudoku::unsetted` error: ld returned 1 exit status
You will need to define the static variable, even if it is not initialized explicitly. That is what is missing in your code. You should have provided a simple example to reproduce the issue, but for your convenience I am providing one which works.
main.cpp
class Foo {
public:
static int si;
static void bar();
};
int Foo::si = 0; // By default, it will be initialized to zero though.
void Foo::bar() {
Foo::si = 10;
};
int main()
{
Foo::bar();
return 0;
}
Note: I would suggest to get someone to review your code because "unsetted" is incorrect English. If we are at it, you would probably need to fix your indentation as well.
In your code there is no definition of unsetted, there is only declaration.
The solution is to put somewhere in your cpp file a line like this:
int Sudoku::unsetted
The reason for that is that each instantiation of Sudoku class will use the same unsetted member so it cannot be defined for each of them, so it's up to programmer to define it in one place only.
In your cpp file, define the static variable (ideally with an initialization):
int Sudoku::unsetted = 0;
If you are declaring any static variable in class, then you should define that variable outside the class also.
Example:
class A
{
public:
static int x; // declaration
};
int A::x; // definition

What does "void-value is not ignored" error mean and how to remove it?

I try to compile the following code:
#include <cppunit/extensions/HelperMacros.h>
#include "tested.h"
class TestTested : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestTested);
CPPUNIT_TEST(check_value);
CPPUNIT_TEST_SUITE_END();
public:
void check_value();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestTested);
void TestTested::check_value() {
tested t(3);
int expected_val = t.getValue(); // <----- Line 18.
CPPUNIT_ASSERT_EQUAL(7, expected_val);
}
As a result I get:
testing.cpp:18:32: Error: void-value is not ignored where it should be
EDDIT
To make the example complete I post the code of the tested.h and tested.cpp:
tested.h
#include <iostream>
using namespace std;
class tested {
private:
int x;
public:
tested(int int_x);
void getValue();
};
tested.cpp
#include <iostream>
using namespace std;
tested::tested(int x_inp) {
x = x_inp;
}
int tested::getValue() {
return x;
}
you declare void getValue(); in the class tested.. change to int getValue();.
A void function cannot return a value.
You are getting a value of int from the API getValue(), hence it should return an int.
Your class definition doesn't match the implementation:
In your header you've declared it in the following way (as an aside, you might want to look into some naming conventions).
class tested {
private:
int x;
public:
tested(int int_x);
void getValue();
};
You've declared getValue() as void, i.e no return. Doesn't make much sense for a getter to return nothing, does it?
However, in the .cpp file you've implemented getValue() like so:
int tested::getValue() {
return x;
}
You need to update the getValue() method signature in the header type so that its return type matches the implementation (int).