virtual method of header give errors during g++ compilation - c++

I'm initiating to C++ and I'm struggling with a compiling problem
I have a source file "binomial.cpp" in which I define the methods of my classes :
#include "binomial.hpp"
using namespace std;
int Bernoulli::operator()(){
return (rand() < p*RAND_MAX) ? a : b;
};
int Binomial::operator()(){
int result(0);
for(int i(0);i<n;++i){
int a;
a = B();
result += a;
};
return result;
};
and a header file "binomial.hpp" where I declare all my classes :
#include <iostream>
#ifndef BINOMIAL
#define BINOMIAL
class RandVar {
virtual int operator()() =0;
};
struct Bernoulli : public RandVar {
Bernoulli(int a = -1,int b = 1, double p = 0.5) : a(a), b(b), p(p) {};
int operator()(){};
private:
int a,b;
double p;
};
class Binomial : public RandVar {
public:
Binomial(Bernoulli B, int n=2)
: B(B), n(n) {}
int operator()(){};
private:
Bernoulli B;
int n;
};
#endif
But when I try to compile that through g++ using the command : g++ -Wall binomial.cpp -o binomial those errors occur :
binomial.hpp: In member function ‘virtual int Bernoulli::operator()()’:
binomial.hpp:14:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.hpp: In member function ‘virtual int Binomial::operator()()’:
binomial.hpp:26:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.cpp: At global scope:
binomial.cpp:4:5: error: redefinition of ‘int Bernoulli::operator()()’
int Bernoulli::operator()(){
^~~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:14:6: note: ‘virtual int Bernoulli::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
binomial.cpp:8:5: error: redefinition of ‘int Binomial::operator()()’
int Binomial::operator()(){
^~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:26:6: note: ‘virtual int Binomial::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
I don't really know how to fix that so if someone can take some time to help a beginner it would be great !

You should replace both
int operator()(){};
with
int operator()();
in the header files. You meant to provide declarations, not definitions. To provide just a declaration (not provide the code right away), just drop the {}.

Related

C++ compile error, after fixing The include file error [duplicate]

This question already has answers here:
What is the difference between private and protected members of C++ classes?
(19 answers)
What are public, private and protected in object oriented programming?
(7 answers)
Closed 1 year ago.
Dear all I have this code running on visual studio . I am getting error as below
I have taken example from books. I have installed visual studio recently. I have tested basic code working well.
I am getting Error for Include files so try to correct Include path error. Kindly suggest me what can be done
Link video
IntialError
Correction Made
#include <iostream>
#include <cstring>
using namespace std;
class BaseClass1
{
private:
int Num1;
public:
void GetNumber(int);
};
class BaseClass2
{
private:
int Num2;
public:
void GetNumber1(int);
};
class Derived :public BaseClass1,public BaseClass2
{
public:
void Display_Result(void);
};
//************************************************************
void BaseClass2::GetNumber1(int b)
{
Num2=b;
}
//************************************************************
void BaseClass1::GetNumber(int a)
{
Num1=a;
}
//************************************************************
void Derived::Display_Result()
{
cout<<"Num1"<<Num1<<endl;
cout<<"Num2"<<Num2<<endl;
cout<<"Result"<<(Num2*Num1)<<endl;
}
int main(int argc, char** argv) {
Derived D;
D.GetNumber(50);
D.GetNumber1(100);
D.Display_Result();
return 0;
}
Error I am getting
C:\Desktop\C++ coding\Visual_basic> cd "c:Desktop\C++ coding\Visual_basic\" ; if ($?)
{ g++ Inheitance.cpp -o Inheitance } ; if ($?) { .\Inheitance }
Inheitance.cpp: In member function 'void Derived::Display_Result()':
Inheitance.cpp:55:16: error: 'int BaseClass1::Num1' is private within this context
cout<<"Num1"<<Num1<<endl;
^~~~
Inheitance.cpp:9:7: note: declared private here
int Num1;
^~~~
Inheitance.cpp:56:16: error: 'int BaseClass2::Num2' is private within this context
cout<<"Num2"<<Num2<<endl;
^~~~
Inheitance.cpp:17:7: note: declared private here
int Num2;
^~~~
Inheitance.cpp:57:19: error: 'int BaseClass2::Num2' is private within this context
cout<<"Result"<<(Num2*Num1)<<endl;
^~~~
Inheitance.cpp:17:7: note: declared private here
int Num2;
^~~~
Inheitance.cpp:57:24: error: 'int BaseClass1::Num1' is private within this context
cout<<"Result"<<(Num2*Num1)<<endl;
^~~~
Inheitance.cpp:9:7: note: declared private here
int Num1;
^~~~

C++ why am I getting this strange error for no matching function call

I have the following code segment:
UnaryExpression.h:
#ifndef UNARYEXPRESSION_H_
#define UNARYEXPRESSION_H_
#include "Expression.h"
class UnaryExpression : public Expression {
private:
Token op;
Expression exp;
protected:
std::map<std::string, float> scope;
public:
UnaryExpression(std::map<std::string, float> &scope, Token& op, Expression& exp);
virtual ~UnaryExpression();
void evaluate();
float getReturnValue();
};
#endif /* UNARYEXPRESSION_H_ */
UnaryExpression.cpp:
#include "UnaryExpression.h"
UnaryExpression::UnaryExpression(std::map<std::string, float> &scope, Token& op, Expression& exp) : Expression(scope) {
this->op = op;
this->exp = exp;
}
UnaryExpression::~UnaryExpression() {}
void UnaryExpression::evaluate() {}
float UnaryExpression::getReturnValue() {
return this->returnResult;
}
I am constantly getting an error, but as soon as I remove Expression exp; from the constructor and private variables, the error seems to dissapear and I'm not sure why this is happening:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\UnaryExpression.o" "..\\src\\UnaryExpression.cpp"
..\src\UnaryExpression.cpp: In constructor 'UnaryExpression::UnaryExpression(std::map<std::__cxx11::basic_string<char>, float>&, Token&, Expression&)':
..\src\UnaryExpression.cpp:10:117: error: no matching function for call to 'Expression::Expression()'
10 | UnaryExpression::UnaryExpression(std::map<std::string, float> &scope, Token& op, Expression& exp) : Expression(scope) {
| ^
In file included from ..\src\UnaryExpression.h:11,
from ..\src\UnaryExpression.cpp:8:
..\src\Expression.h:18:2: note: candidate: 'Expression::Expression(std::map<std::__cxx11::basic_string<char>, float>&)'
18 | Expression(std::map<std::string, float> &scope);
| ^~~~~~~~~~
..\src\Expression.h:18:2: note: candidate expects 1 argument, 0 provided
..\src\Expression.h:13:7: note: candidate: 'Expression::Expression(const Expression&)'
13 | class Expression : public Statement {
| ^~~~~~~~~~
..\src\Expression.h:13:7: note: candidate expects 1 argument, 0 provided
Build Failed. 1 errors, 0 warnings. (took 1s.89ms)
For reference, here is the definition of Expression.h:
#ifndef EXPRESSION_H_
#define EXPRESSION_H_
#include "Statement.h"
class Expression : public Statement {
protected:
std::map<std::string, float> scope;
float returnResult;
public:
Expression(std::map<std::string, float> &scope);
virtual ~Expression();
virtual void evaluate();
virtual float getReturnValue();
};
#endif /* EXPRESSION_H_ */
The class Statement is similar to Expression, it has only a virtual method void evaluate() and a scope. When I initialize Expression, of course I pass the scope to the parent Statement class.

Error binding properties and functions in emscripten

I'm trying to use emscripten to compile a c++ class and expose bindings. I'm running into an error from the compiler.
#include <emscripten/bind.h>
#include <emscripten/emscripten.h>
using namespace emscripten;
class MyClass {
private:
int _year;
int _month;
int _day;
public:
MyClass() { }
MyClass(int year, int month, int day);
int getMonth();
void setMonth(int);
int getYear();
void setYear(int);
int getDay();
void setDay(int);
bool isLeapYear();
int daysInMonth();
void increment();
void decrement();
};
EMSCRIPTEN_BINDINGS(my_sample_class) {
class_<MyClass>("MyClass")
.constructor<>()
.constructor<int, int, int>()
.function("getMonth", &MyClass::getMonth)
.function("increment", &MyClass::increment)
.function("decrement", &MyClass::decrement)
.property("year",&MyClass::getYear, &MyClass::setYear )
//.property("month", &MyClass::getMonth, &MyClass::setMonth )
//.property("day",&MyClass::getDay, &MyClass::setDay )
;
}
The compiler has no problems with the constructors or the function binding. I run into a problem with the property binding. I only have one uncommented to minimize the errors that I get back (they are just repeats of the same error but for different lines). Here are the errors that I'm getting back.
In file included from MyDate.cpp:1:
In file included from ./MyDate.h:2:
emscripten/bind.h:1393:26: error: implicit instantiation of undefined template 'emscripten::internal::GetterPolicy<int (MyClass::*)()>'
auto gter = &GP::template get<ClassType>;
^
./MyDate.h:37:6: note: in instantiation of function template specialization 'emscripten::class_<MyClass, emscripten::internal::NoBaseClass>::property<int (MyClass::*)(), void (MyClass::*)(int)>' requested here
.property("year",&MyClass::getYear, &MyClass::setYear )
^
emscripten/bind.h:569:16: note: template is declared here
struct GetterPolicy;
^
emscripten/bind.h:1399:33: error: implicit instantiation of undefined template 'emscripten::internal::GetterPolicy<int (MyClass::*)()>'
TypeID<typename GP::ReturnType>::get(),
^
emscripten\1.38.21\system\include\emscripten/bind.h:569:16: note: template is declared here
struct GetterPolicy;
^
2 errors generated.
shared:ERROR: compiler frontend failed to generate LLVM bitcode, halting
I've looked up binding examples and it appears I'm using the right syntax. Does any one have any idea of what I might be doing wrong?
Found the problem!
The getter functions must be marked as const to avoid these errors.
EX:
int getMonth() const;

In Constructor ClassName::ClassName() ... ClassName doesn't have any field named 'VariableName'

I'm dealing with an issue right now in C++, where I have a defined variable existing within my class definition, the compiler gets angry and tells me that said variable has not been defined within the scope, and that the constructor doesn't have any field for the variable in question.
I have a simple class hierarchy where I have the base class, Neuron, and two subclasses HiddenNeuron and OutputNeuron, that are both inheriting from Neuron in public virtual mode.
I've made a neuron.h file where I defined the virtual header class, and as such, I have been told by my instructor that I need not write an implementation file for it.
I've then written two header files for HiddenNeuron and OutputNeuron, in which I define them, and two corresponding cpp files that implement them, respectively.
When I try running
g++ neuralnet.cpp input_neuron.cpp hidden_neuron.cpp output_neuron.cpp
I get a bunch of errors like:
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
and
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
Here's the code for my classes:
neuron.h:
#ifndef NEURON_H
#define NEURON_H
class Neuron {
//protected:
// double long value;
public:
Neuron(void);
//Neuron(const double long val);
//
virtual double long activation(void) const =0;
virtual void input(double long val);
};
#endif
output_neuron.h:
#ifndef OUTPUT_NEURON_H
#define OUTPUT_NEURON_H
#include "neuron.h"
class OutputNeuron : public virtual Neuron {
private:
double long value;
public:
OutputNeuron(void);
OutputNeuron(const double long val);
// this should return a value between 0 & 1
virtual double long activation(void) const;
// vector multiplication of the weight vector
// and all the values of the last layer
// with an additional corresponding bias
// added onto the end.
virtual void input(double long val);
};
#endif
output_neuron.cpp:
#include "output_neuron.h"
#include <cmath>
OutputNeuron::OutputNeuron(void) : value(0) { }
OutputNeuron::OutputNeuron(const double long val)
: value(val) { }
inline double long OutputNeuron::activation(void) const {
return 1 / (1 + exp((-1)*value));
}
inline void OutputNeuron::input(double long val) {
value = val;
}
hidden_neuron.h:
#ifndef HIDDEN_NEURON_H
#define HIDDEN_NEURON_H
#include "neuron.h"
class HiddenNeuron : public virtual Neuron {
private:
double long value;
public:
HiddenNeuron(void);
HiddenNeuron(const double long val);
// activation function that takes the stored value
// and squashes it and returns it
virtual double long activation(void) const;
virtual void input(double long val);
};
#endif
hidden_neuron.cpp:
#include "hidden_neuron.h"
#include <cstdlib>
#include <cmath>
HiddenNeuron::HiddenNeuron(void)
: value(pow(-1, rand()) * static_cast<double>(rand())/static_cast<double>(RAND_MAX)){}
HiddenNeuron::HiddenNeuron(const double long val) : value(val) { }
inline void HiddenNeuron::input(double long val) { value = val; }
inline double long HiddenNeuron::activation(void) const {
return 1/(1 + exp((-1)*value));
}
FULL ERROR LOG:
neuralnet.cpp: In member function ‘void NeuralNet::initialize(bool)’:
neuralnet.cpp:72:18: error: invalid new-expression of abstract class type ‘HiddenNeuron’
temp = new HiddenNeuron;
^~~~~~~~~~~~
In file included from neuralnet.h:5:0,
from neuralnet.cpp:1:
hidden_neuron.h:5:7: note: because the following virtual functions are pure within ‘HiddenNeuron’:
class HiddenNeuron : public virtual Neuron {
^~~~~~~~~~~~
In file included from hidden_neuron.h:3:0,
from neuralnet.h:5,
from neuralnet.cpp:1:
neuron.h:14:16: note: virtual void Neuron::input(long double) const
virtual void input(double long val) const =0;
^~~~~
neuralnet.cpp:85:18: error: invalid new-expression of abstract class type ‘HiddenNeuron’
temp = new HiddenNeuron;
^~~~~~~~~~~~
neuralnet.cpp:98:18: error: invalid new-expression of abstract class type ‘OutputNeuron’
temp = new OutputNeuron;
^~~~~~~~~~~~
In file included from neuralnet.h:7:0,
from neuralnet.cpp:1:
output_neuron.h:5:7: note: because the following virtual functions are pure within ‘OutputNeuron’:
class OutputNeuron : public virtual Neuron {
^~~~~~~~~~~~
In file included from hidden_neuron.h:3:0,
from neuralnet.h:5,
from neuralnet.cpp:1:
neuron.h:14:16: note: virtual void Neuron::input(long double) const
virtual void input(double long val) const =0;
^~~~~
hidden_neuron.cpp: In constructor ‘HiddenNeuron::HiddenNeuron()’:
hidden_neuron.cpp:6:5: error: class ‘HiddenNeuron’ does not have any field named ‘value’
: value(pow(-1, rand()) * static_cast<double>(rand())/static_cast<double>(RAND_MAX)){}
^~~~~
hidden_neuron.cpp: In constructor ‘HiddenNeuron::HiddenNeuron(long double)’:
hidden_neuron.cpp:8:53: error: class ‘HiddenNeuron’ does not have any field named ‘value’
HiddenNeuron::HiddenNeuron(const double long val) : value(val) { }
^~~~~
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
^~~~~
hidden_neuron.cpp:10:52: note: suggested alternative: ‘val’
inline void HiddenNeuron::input(double long val) { value = val; }
^~~~~
val
hidden_neuron.cpp: In member function ‘virtual long double HiddenNeuron::activation() const’:
hidden_neuron.cpp:13:26: error: ‘value’ was not declared in this scope
return 1/(1 + exp((-1)*value));
^~~~~
hidden_neuron.cpp:13:26: note: suggested alternative: ‘valloc’
return 1/(1 + exp((-1)*value));
^~~~~
valloc
output_neuron.cpp: In constructor ‘OutputNeuron::OutputNeuron()’:
output_neuron.cpp:4:36: error: class ‘OutputNeuron’ does not have any field named ‘value’
OutputNeuron::OutputNeuron(void) : value(0) { }
^~~~~
output_neuron.cpp: In constructor ‘OutputNeuron::OutputNeuron(long double)’:
output_neuron.cpp:7:5: error: class ‘OutputNeuron’ does not have any field named ‘value’
: value(val) { }
^~~~~
output_neuron.cpp: In member function ‘virtual long double OutputNeuron::activation() const’:
output_neuron.cpp:10:28: error: ‘value’ was not declared in this scope
return 1 / (1 + exp((-1)*value));
^~~~~
output_neuron.cpp:10:28: note: suggested alternative: ‘valloc’
return 1 / (1 + exp((-1)*value));
^~~~~
valloc
output_neuron.cpp: In member function ‘virtual void OutputNeuron::input(long double)’:
output_neuron.cpp:14:3: error: ‘value’ was not declared in this scope
value = val;
^~~~~
output_neuron.cpp:14:3: note: suggested alternative: ‘val’
value = val;
^~~~~
val
If anyone can help me, I've been pulling my hair out with this issue.
Edit: using class design as it's a requirement for the course I'm currently enrolled in.
Edit 2: included the full error log
Edit 3: Okay so this is really bizarre, what I wrote works in a separate project directory as pointed out by user idontseethepoint, however when it's in the same directory as my main project, it still gives me the exact same error.

Call of overloaded function is ambiguous although different namespace

I do understand why the following would be a problem if no namespaces were used. The call would be ambiguous indeed. I thought "using stD::swap;" would define which method to use.
Why does it work for "int" but not a "class"?
#include <memory>
namespace TEST {
class Dummy{};
void swap(Dummy a){};
void sw(int x){};
}
namespace stD {
void swap(TEST::Dummy a){};
void sw(int x){};
class aClass{
public:
void F()
{
using stD::swap;
TEST::Dummy x;
swap(x);
}
void I()
{
using stD::sw;
int b = 0;
sw(b);
}
};
}
This is the error message:
../src/Test.h: In member function ‘void stD::aClass::F()’:
../src/Test.h:26:9: error: call of overloaded ‘swap(TEST::Dummy&)’ is ambiguous
swap(x);
^
../src/Test.h:26:9: note: candidates are:
../src/Test.h:17:6: note: void stD::swap(TEST::Dummy)
void swap(TEST::Dummy a){};
^
../src/Test.h:10:6: note: void TEST::swap(TEST::Dummy)
void swap(Dummy a){};
^
I thank you very much in advance for an answer.
This line is using argument dependent lookup
TEST::Dummy x;
swap(x);
So it will find both void stD::swap(TEST::Dummy) as well as void TEST::swap(TEST::Dummy) because x carries the TEST:: namespace.
In the latter case int b = 0; the variable b is not in a namespace, so the only valid function to call would be stD::sw due to your using statement.