Recursively add array numbers with inner class functions c++ - c++

Having some trouble figuring out what this error is.
#include <iostream>
class listSolver{
private:
int arraySize;
int *address;
public:
listSolver(int *arrayAddress, int size);
~listSolver();
int forLoopSolver();
int whileLoopSolver();
int recursiveSolver();
int addNumbers(int *numAddress,int num, int count);
};
listSolver::listSolver(int *arrayAddress,int size){
arraySize = size;
address = arrayAddress;
}
listSolver::~listSolver(){
}
int listSolver::recursiveSolver(){
int *funcAddress = address;
int size = arraySize;
int solution = 0;
return this->addNumbers(funcAddress,size,solution);
}
int addNumbers(int *numAddress,int num, int count){
if(count == 0){
return num+*numAddress;
}
else{
count--;
num+=*numAddress;
numAddress++;
return listSolver::addNumbers(numAddress,num,count);
}
}
g++ compiler gives me
/tmp/ccuy9bHM.o: In function 'listSolver::recursiveSolver()':
problem1.cpp:(.text+0x1e2): undefined reference to 'listSolver::addNumbers(int*, int, int)'
collect2: error: ld returned 1 exit status
Not sure what it means. Been trying different cominations of calling the function and can't get it to work.

int ListSolver::addNumbers(int *numAddress,int num, int count){
^^^^^^^^^^^^
Need the class name with the scope operator ("four dots" as we call it here for ages - TY panta rhei for the spot) to define a member function outside a class body. The way it is, you've defined a global function int addNames(/* ... */) that compiles successfully per se, but since you are trying to call the said member function, at link time, an error occurs.

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

Determine the struct member through function parameter

I want to create a function which will assign the value of the struct array and it will determine the member of struct through its parameter.
I mean instead of creating seperate function for each member of the struct, determine the member through the function parameter(examples : &.tests, lessons.exams)
The Code ı wrote down is only for explain what I mean, values can be imported from a text file instead of assigning them by random.
What I want to understand is; is there any other way to call struct member without writing its name?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct lsn
{
char name[20];
int tests[4];
int quizzes[4];
int exams[4];
int finals[4];
};
void random_notes(lsn *x, int *y)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].y[j]=rand()%101;
}
int main()
{
srand(time(NULL));
lsn lessons[30];
random_notes(lessons, &.tests);
random_notes(lessons, &.quizzes);
random_notes(lessons, &.exams);
random_notes(lessons, &.finals);
return 0;
}
Instead of creating 4 functions as below,
void random_tests(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].tests[j]=rand()%101;
}
void random_quizzes(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].quizzes[j]=rand()%101;
}
void random_exams(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].exams[j]=rand()%101;
}
void random_finals(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].finals[j]=rand()%101;
}
Just one function that determine the struct member throuh its parameter,
void random_notes(lsn *x, .struct_member y)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].y[j]=rand()%101;
}
In this example the function is very small, but imagine a huge code in a fuction, only the struct member is different and rest of the code is same.
Yes, C++ has a concept of "pointers to members". This will allows you to pass the identity of the member you wish to initialize. The syntax is a bit wonky however, so beware:
void random_notes(lsn *x, int (lsn::* y)[4])
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
(x[i].*y)[j]=rand()%101; // << Access the member of x[i] via y
}
Which is to be called like this:
random_notes(lessons, &lsn::tests);
Pass a function which when called returns the appropriate struct member. For example:
random_notes(lessons, [=](lsn& lesson) { return lesson.quizzes; });
in random_notes function, you just call the function with a lsn instance and it will give you the array to populate

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

undefined reference with member functions using g++

For some reasons, sometimes when I define my member functions in a CPP file rather than within their declaring header, I get undefined reference errors from g++.
This question is similar to Undefined Reference To Member function but that user managed to solve the problem by adding the missing file to his command line, which doesn't seem to be working here.
Btw, I'm not against using a makefile; in fact I plan to do so eventually, whenever I get used to these commands.
my main file ('ringbuftest.cpp'):
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <cstring>
#include "ringbuffer.h"
using namespace std;
void *reader(void* param);
void *writer(void* param);
struct pack{
char msg[256];
};
RINGBUFFER<pack> *ringo;
int main(){
pthread_t rdr;
pthread_t wtr;
ringo = new RINGBUFFER<pack>(12);
int ret1 = pthread_create(&rdr,NULL,reader,(void*)NULL);
int ret2 = pthread_create(&wtr,NULL,writer,(void*)NULL);
#ifdef _unix
cout<< "unix single underscore\n";
#elif defined __unix
cout<< "unix two underscores\n";
#endif
pthread_join(wtr,NULL);
pthread_join(rdr,NULL);
cout<< "threads are done\n";
exit(0);
return 0;
}
void *reader(void *param){
pack myPack;
while(true)
{
for(int i=0;i<10000;i++){int k=0;k=i;k++;i=k;i--;}
if( ringo->Pop(&myPack) )
{
cout<< myPack.msg;
}
}
}
void *writer(void *param){
pack myPack;
while(true){
strcpy(myPack.msg,"hello reader!\n");
ringo->Push(&myPack);
}
}
my class header ('ringbuffer.h'):
#include<stdlib.h>
using namespace std;
#ifndef __ring_buffer_h__
#define __ring_buffer_h__
#define RINGBUFFER_DEFAULT_SIZE 8
template <class T>
class RINGBUFFER
{
private:
unsigned int top;
unsigned int bottom;
unsigned int size;
unsigned int count;
void *items;
public:
RINGBUFFER();
RINGBUFFER(unsigned int size);
~RINGBUFFER();
bool Push(T *value);
bool Pop(T *value);
};
#endif
my class definitions ('ringbuffer.CPP'):
#include "ringbuffer.h"
template<class T>
RINGBUFFER<T>::RINGBUFFER()
{
top = bottom = 0;
size = RINGBUFFER_DEFAULT_SIZE;
count = 0;
items = malloc((size+1)*sizeof(T));
}
template<class T>
RINGBUFFER<T>::RINGBUFFER(unsigned int _size)
{
top = bottom = 0;
size = _size;
count = 0;
items = malloc(size*sizeof(T));
}
template<class T>
RINGBUFFER<T>::~RINGBUFFER()
{
free(items);
}
template<class T>
bool RINGBUFFER<T>::Push(T *value)
{
if( count<size )
{
memcpy(&(((T*)items)[bottom]),value,sizeof(T));
bottom = (bottom+1)%size;
count++;
return true;
}
return false;
}
template<class T>
bool RINGBUFFER<T>::Pop(T *value)
{
if( count>0 )
{
memcpy(value,&(((T*)items)[top]),sizeof(T));
top = (top+1)%size;
count--;
return true;
}
return false;
}
To compile, I've been trying to use:
g++ ringbuffer.CPP ringbuftest.cpp -lpthread -o ringbuffertest.o
and I get the errors:
/tmp/ccj8RqhY.o: In function `main':
ringbuftest.cpp:(.text+0x21): undefined reference to `RINGBUFFER<pack>::RINGBUFFER(unsigned int)'
/tmp/ccj8RqhY.o: In function `reader(void*)':
ringbuftest.cpp:(.text+0x157): undefined reference to `RINGBUFFER<pack>::Pop(pack*)'
/tmp/ccj8RqhY.o: In function `writer(void*)':
ringbuftest.cpp:(.text+0x1db): undefined reference to `RINGBUFFER<pack>::Push(pack*)'
collect2: error: ld returned 1 exit status
I'm guessing I'm doing something wrong with g++ because I keep hitting this problem in different projects, or maybe I'm using templates wrong.(?)
How can I resolve this error?
Edit:
I should mention that this compiles perfectly if I instead paste the member definitions into the header file and exclude the .CPP from the g++ command.
You need to move the entire definition of RINGBUFFER<T> from ringbuffer.cpp to ringbuffer.h so that it's visible when ringbuftest.cpp is being compiled.

Error: "undefined reference to 'function'" in C++

I got an error while compiling C++:
/tmp/ccqs6UN2.o: In function main': PowerModulus.cpp:(.text+0x194): undefined reference to takeModulusLOOP(int, int, int)' collect2: ld returned 1 exit status
The source code:
#include "PowerModulus.h"
#include <iostream>
int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);
int main() {
std::cout << takeModulusLOOP(5348, 700, 335);
}
int PowerModulus::takeModulusLOOP(int x, int n, int moduint) {
int total = modint(x, moduint);
n--;
while (--n) {
total = modint(total * x, moduint);
}
return total;
}
int PowerModulus::modint(int x, int moduint) {
while (x < 0) // Deal with negative
x += moduint;
return x % moduint; // Comes out positive now -> %
}
PowerModulus::PowerModulus() {
// TODO Auto-generated constructor stub
}
PowerModulus::~PowerModulus() {
// TODO Auto-generated destructor stub
}
Header file:
#ifndef POWERMODULUS_H_
#define POWERMODULUS_H_
int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);
class PowerModulus {
public:
int takeModulusLOOP(int x, int n, int moduint);
int modint(int x, int moduint);
PowerModulus();
virtual ~PowerModulus();
};
#endif /* POWERMODULUS_H_ */
Where is the error?
You have declared a global takeModulusLOOP function, then call it in main, without ever defining it. This is a different function than PowerModulus::takeModulusLOOP.
// main.cpp
#include "PowerModulus.h"
#include <iostream>
int main(){
std::cout << PowerModulus::takeModulusLOOP(5348,700,335) << '\n';
return 0;
}
Changed to a namespace instead of a class, and separated into header and implementation (instead of grouping in main.cpp):
// PowerModulus.cpp
#include "PowerModulus.h"
namespace PowerModulus {
int takeModulusLOOP(int x, int n, int moduint){
int total = modint(x, moduint) ;
n--;
while (--n){
total = modint( total * x, moduint );
}
return total;
}
int modint(int x, int moduint){
while ( x < 0) // deal with negative
x += moduint;
return x % moduint;//comes out positive now -> %
}
}
Header:
// PowerModulus.h
#ifndef POWERMODULUS_H_
#define POWERMODULUS_H_
namespace PowerModulus {
int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);
}
#endif
This line:
std::cout << takeModulusLOOP(5348,700,335);
is calling the non-class takeModulusLOOP, which you haven't defined anywhere.
You should either call the class version, by providing an object of the class type and using something like:
PowerModulus p;
std::cout << p.takeModulusLOOP(5348,700,335);
(most likely) or providing a non-class version (least likely).
You could also consider making the function static since it doesn't seem to require an object at all. Then you don't need to instantiate one.
You receive the error, because you do not have such a function.
Actually, you have it in PowerModulus class, so you should call the function from PowerModulus instance.
PowerModulus pM;
pM.takeModulusLoop(5348,700,335);
You do not need to claim the function in the beginning of your .h file or in the beginning of your .cpp file.
If you intended to use the takeModulusLoop function of the PowerModulus class then you need not declare a global function again...
But, if you intended to use a different global function, then you need to define it in its context...