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...
Related
I want to make a overloading function with a prototype in C++.
#include <iostream>
using namespace std;
int rectangle(int p, int l);
int main() {
cout << rectangle(3);
return 0;
}
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
I got error at
int rectangle(int p, int l);
is that possible make prototype with a overloading function? if possible how to do it
You've to declare the function before you use/call it. You did declare the 2 argument version of rectangle function but you seem to forget to declare the 1 argument taking version.
As shown below if you add the declaration for the 1 argument version then your program works(compiles).
#include <iostream>
using namespace std;
//declare the function before main
int rectangle(int p, int l);
int rectangle(int p);//ADDED THIS DECLARATION
int main() {
cout << rectangle(3);
return 0;
}
//define the functions after main
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
The output of the program can be seen here.
Alternative solution:
If you don't want to declare each function separately then you should just define them before main instead of declaring them as shown below.
#include <iostream>
using namespace std;
//define the functions before main. This way there is no need to write a separate function declaration because all definition are declarations
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
int main() {
cout << rectangle(3);
return 0;
}
I just noticed something when creating functions. In the code:
#include <iostream>
using namespace std;
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
it will work because the function being called is on top of the caller, but if I put the function add() below the calling function in main() it won't work.
#include <iostream>
using namespace std;
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
and the compiler will tell me that the identifier add() cannot be found.
so why do we declare functions anyway? like this:
#include <iostream>
using namespace std;
int add(int a, int b = 20);
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b)
{
int r;
r = a + b;
return (r);
}
A definition is implicitly a declaration. And a declaration must come ahead of the use.
All functions need to be declared before they are used.
You can do that by either (1) writing a declaration, or (2) writing a definition.
Relying solely on (2) can be tempting but then you are bound to order your program in a particular way, and is occasionally impossible. For example, the following will not compile unless the comment is removed.
//void bar(int);
void foo(int n)
{
if (!n){
bar(n);
}
}
void bar(int n)
if (n){
foo(n);
}
}
int main()
{
foo(1);
}
No.
If the function definition appears before the function call, then prototype is not mandatory. Otherwise function prototype is necessary to let compiler know how to respond to a function when it is called.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
if the function definition appears after the function call then prototype is mandatory. because it tells the compiler to how to respond the function when it is called.
check the following example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int, int); // function prototype
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
and if the function definition is made before the function call then it is not mandatory to declare function prototype.
consider example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}
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.
I have the following problem.
I have a base abstract class with a pure virtual method, and I want to pass it as an argument to another member function(so not a normal function). Yet I have an error when trying to call the method with specified function. Code speaks better than words so bellow I have posted the code that generates the problem
class BaseClass
{
public:
BaseClass();
int add(int, int);
virtual void op(void (*f)(int, int), string s, int a, int b) = 0;
~BaseClass();
};
#include "BaseClass.h"
class ClasaDerviata:public BaseClass
{
public:
ClasaDerviata();
void testNumere(int a, int b);
void op(void(*f)(int, int), string s, int a, int b);
~ClasaDerviata();
};
#include "BaseClass.h"
BaseClass::BaseClass()
{
}
int BaseClass::add(int a, int b)
{
return a + b;
}
BaseClass::~BaseClass()
{
}
#include "ClasaDerviata.h"
#include <iostream>
using namespace std;
ClasaDerviata::ClasaDerviata()
{
}
void ClasaDerviata::testNumere(int a, int b)
{
cout << a + b << "\n";
cout << " suma " << add(a,b) << "\n";
}
void ClasaDerviata :: op (void (*f)(int, int), string s, int a, int b)
{
f(a, b);
cout << s << "\n";
}
ClasaDerviata::~ClasaDerviata()
{
}
#include "ClasaDerviata.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ClasaDerviata *a;
a = new ClasaDerviata();
int x, y;
cin >> x >> y;
a->op(&ClasaDerviata::testNumere, "test metoda", x, y);
system("pause");
return 0;
}
Thank you for your time!
void ClasaDerviata::testNumere(int a, int b); is not of type void (*)(int, int) but void (ClasaDerviata::*)(int, int)
You may add static to testNumere and add to fix your problem or change signature of your function (and change internal code too).
Remember when you make a call to a member function a hidden parameter 'this' is passed. So your ClasaDerviata::testNumere(int a, int b); function actually takes three parameters.
I would suggest to read Joseph Garvin comment in
How can I pass a class member function as a callback?
he has explained it very well.
I new in C++ and I have difficulty to understand how to get my function with inheritance.
I have a Class that is link to another with inheritance, everything work except:
I cannot reach my superclass function.
Here's my class header : Point.h (I don't include the .cpp):
#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
public:
Point();
void set_values (int , int);
void set_values (int , int , int );
void affichervaleurs();
int getX() const { return x; }
int getY() const { return y; }
private:
int x ;
int y ;
int z ;
};
#endif
Now My other class that try to access the function getX from Point.h :
The header : Carre.h
#ifndef Carre_H
#define Carre_H
#include "Point.h"
class Carre : public Point{
public:
Carre();
//Carre(int a , int b);
//Carre(int a, int b):Point(a,b) {};
//Carre(int a, int b, int c):Point(a, b, c) {};
//const Point &pp;
int Aire (){
};
void affichercar(){
};
};
#endif
Carre.cpp
#include <iostream>
using namespace std;
#include "Carre.h"
#include "Point.h"
Carre::Carre():Point(){
};
//Carre::Carre(int a, int b);
//const &pp;
int Aire (){
return (getX() * getY());
};
void affichercar(){
//cout << "Coordonnees X:" << x << endl;
};
It says that my GetX() is undeclared in my Carre.cpp .
Like I said I'm new in C++
Does someone know what I'm missing to make that code work. ?
Your definition is missing the class scope, which makes it a free function instead of a member.
It should be
int Carre::Aire (){
return getX() * getY();
};
In the .cpp file for Carre, the functions Aire and affichercar are global. Presumably you intended:
int Carre::Aire(){
return (getX() * getY());
};
For example.
Declaring function outside class body requires a class specifier:
int Carre::Aire () {
return (getX() * getY());
};
void Carre::affichercar() {
//...
}
Otherwise
int Aire () {
return (getX() * getY());
};
is just another function in global namespace that can exists simutaneously to Carre::Aire().
This is because you are not implementing the Aire function as being part of the Carre class.
Try changing
int Aire (){
to
int Carre::Aire (){
Also, you already have an implementation of the Aire method in the header file. You should either implement the function inline in the header file, or in the .cpp file, but not both. This also applies to your affichercar method.