I am trying to implement a class in c++ but I keep getting an error.
My .cpp looks like:
#include "medianfinderheader.h"
MedianFinder::MedianFinder() {
}
void MedianFinder::addNum(int num) {
}
double MedianFinder::findMedian() {
double x=1.0;
return x;
}
while my header file looks like:
class MedianFinder {
public:
MedianFinder() {};
void addNum(int num) {};
double findMedian() {
double x=1.0;
return x;
};
};
However I keep getting the following error :
g++ -I./ -g -Og -std=c++14 main.cpp MedianFinderClass.cpp -o medianEX
MedianFinderClass.cpp:2:5: error: redefinition of 'MedianFinder::MedianFinder()'
2 | MedianFinder::MedianFinder() {
| ^~~~~~~~~~~~
In file included from MedianFinderClass.cpp:1:
medianfinderheader.h:3:5: note: 'MedianFinder::MedianFinder()' previously defined here
3 | MedianFinder() {};
| ^~~~~~~~~~~~
MedianFinderClass.cpp:5:10: error: redefinition of 'void MedianFinder::addNum(int)'
5 | void MedianFinder::addNum(int num) {
| ^~~~~~~~~~~~
In file included from MedianFinderClass.cpp:1:
medianfinderheader.h:4:10: note: 'void MedianFinder::addNum(int)' previously defined here
4 | void addNum(int num) {};
| ^~~~~~
MedianFinderClass.cpp:9:12: error: redefinition of 'double MedianFinder::findMedian()'
9 | double MedianFinder::findMedian() {
| ^~~~~~~~~~~~
In file included from MedianFinderClass.cpp:1:
medianfinderheader.h:6:12: note: 'double MedianFinder::findMedian()' previously defined here
6 | double findMedian() {
I have no idea why this is happening when I've implmented classes like this before any help would be appreciated.
I have implemented classes in c++ in this exact same way, and I'm just really frustrated any help would be greatly appreciated.
this line
void addNum(int num) {};
creates an implementation of addNum with an empty body, you need
void addNum(int num);
same for other functions in that class
Related
newbie here my code doesn't seem to compile in vscode. It give me the desired output while using dev c++. It gives me error while reading from file, writing to a file no problem. I have posted error message below the code.
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
class student{
private:
char name[25];
int id;
int age;
public:
void get(){
cin>>name>>id>>age;
}
void show(){
cout<<name<<id<<age;
}
void write2file(){
ofstream outfile("student.dat",ios::binary|ios::app);
get();
outfile.write(reinterpret_cast<char*>(this),sizeof(*this));
}
void readfromfile(){
ifstream infile("student.dat",ios::binary|ios::in);
while(!infile.eof()){
if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0){
show();
}
}
}
};
int main(){
student s;
s.write2file();
s.readfromfile();
return 0;
}
Here is the error I got when I run the program in VS code, but same program run perfectly in dev c++.
awd.cpp: In member function 'void student::readfromfile()':
awd.cpp:26:76: error: no match for 'operator>' (operand types are 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} and 'int')
26 | if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0){
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
| | |
| | int
| std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}
awd.cpp:26:76: note: candidate: 'operator>(int, int)' (built-in)
26 | if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0){
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
awd.cpp:26:76: note: no known conversion for argument 1 from 'std::basic_istream<char>::__istream_type' {aka 'std::basic_istream<char>'} to 'int'
In file included from C:/msys64/mingw64/include/c++/12.1.0/string:47,
from C:/msys64/mingw64/include/c++/12.1.0/bits/locale_classes.h:40,
from C:/msys64/mingw64/include/c++/12.1.0/bits/ios_base.h:41,
from C:/msys64/mingw64/include/c++/12.1.0/ios:42,
from C:/msys64/mingw64/include/c++/12.1.0/ostream:38,
from C:/msys64/mingw64/include/c++/12.1.0/iostream:39,
from awd.cpp:1:
Better code would be this
void readfromfile(){
ifstream infile("student.dat",ios::binary|ios::in);
while (infile.read(reinterpret_cast<char*>(this),sizeof(*this)) {
show();
}
}
As already pointed out istream::read does not return an integer, which is what the original code seems to be assuming.
Instead, like most I/O functions, the stream itself is returned. In a boolean context this can be used to see if the stream is in a good state. If it is not then the previous I/O operation failed.
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;
^~~~
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.
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 {}.
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.