Requesting a function from another file in a class not working
Clash Royale CLAN TAG#URR8PPP
Requesting a function from another file in a class not working
I have 2 .cpp files called "FactoringProgram.cpp" and "FactoringProgram2nd.cpp", also 1 header file called "FactoringProgram.h". I've already tried searching for this problem on StackOverflow and a couple other sites and haven't found a solution that worked for me. When I try to run this command: "g++ FactoringProgram.cpp FactoringProgram2nd.cpp" I get this error:
FactoringProgram.cpp: In function ‘int main()’: FactoringProgram.cpp:8:11: error: request for member ‘InitialMessage’
in ‘Problem1’, which is of non-class type ‘Factoring()’
Problem1.InitialMessage();
The code for "FactoringProgram.h" is:
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
int InitialMessage();
;
#endif
FactoringProgram.cpp code:
#include <stdio.h>
#include <iostream>
#include "FactoringProgramH.h"
int main()
Factoring Problem1();
Problem1.InitialMessage();
return 0;
FactoringProgram2nd.cpp code:
#include "FactoringProgramH.h"
#include <stdio.h>
#include <iostream>
Factoring::Factoring(int FirstCoefficent=0, char FirstOperator='+',
int SecondCoefficent=1, char SecondOperator='+', int 3rdExpression=1)
: m_FirstCoefficent(FirstCoefficen), m_FirstOperator(FirstOperator),
m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),
m_3rdExpression(3rdExpression);
int Factoring::InitialMessage()
std::cout << "Ok right now your expression is looking like: "
<< FirstCoefficent << "x^2 " << FirstOperator << " " << SecondCoefficent
<< " x" << SecondOperator << " " << 3rdExpression;
Factoring Problem1;
Do you have a typo in your
include
statements? FactoringProgramH.h
instead of FactoringProgram.h
.– Chris Hunt
Aug 12 at 18:16
include
FactoringProgramH.h
FactoringProgram.h
Please remove sublimetext3 tag, it is not relevant to the question
– eneski
Aug 12 at 18:41
@eneski This is where you edit/suggest an edit.
– Passer By
Aug 12 at 18:52
You are absolutely right @PasserBy but I tried many times and question is waiting for approval of another edit for long time
– eneski
Aug 12 at 18:54
3 Answers
3
Your code has several small errors. Here is a version that works (you can compare and see what I had to change). A summary of the changes are:
int InitialMessage()
void InitialMessage()
main.cpp
FactoringProgram.h
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring(int FirstCoefficent = 0, char FirstOperator = '+',
int SecondCoefficent = 1, char SecondOperator = '+', int thirdExpression = 1);
void InitialMessage();
;
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
Factoring::Factoring(int firstCoefficent, char firstOperator, int SecondCoefficent, char SecondOperator, int thirdExpression) :
m_FirstCoefficent(firstCoefficent),
m_FirstOperator(firstOperator),
m_SecondCoefficent(SecondCoefficent),
m_SecondOperator(SecondOperator),
m_3rdExpression(thirdExpression)
void Factoring::InitialMessage()
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
main.cpp
#include <stdio.h>
#include <iostream>
#include "FactoringProgram.h"
int main()
Factoring Problem1;
Problem1.InitialMessage();
return 0;
To this error substitute:
Factoring Problem1();
by:
Factoring Problem1 = Factoring();
The problem is that the compiler is interpreting this line as a function declaration rather than a variable declaration. AS mentioned in the comments, this is known as the most vexing parser problem.
Obs: The code you posted contain many more minor errors.
Why not
Factoring Problem1;
?– Neil Butterworth
Aug 12 at 19:53
Factoring Problem1;
Works just fine as well. The way I wrote it is a bit more generic as it would also work if you needed to pass parameters to the constructor.
– Nogoseke
Aug 12 at 23:26
If you need to pass parameters to the constructors then
Factoring Problem1( 1,2,3 );
- you never need (or should) write it the way you did. And it is not "more generic".– Neil Butterworth
Aug 12 at 23:48
Factoring Problem1( 1,2,3 );
Read the example at most vexing parse and you will see that you can have a problem with that notation.
– Nogoseke
Aug 12 at 23:58
No, you can't. You don't understand the most vexing parse. For example, you should always say
string s( "foobar" );
and not string s = string( "foobar" );
.– Neil Butterworth
Aug 13 at 0:01
string s( "foobar" );
string s = string( "foobar" );
You should define Factoring()
without params and I used headers in FactoringProgram.h
Factoring()
FactoringProgram.h
#include <stdio.h>
#include <iostream>
class Factoring
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
Factoring(int,char,int,char,int);
int InitialMessage();
;
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
int main()
Factoring Problem1 = Factoring();
Problem1.InitialMessage();
system("pause");
return 0;
FactoringProgram2nd.cpp
#include "FactoringProgram.h"
Factoring::Factoring()
*this = Factoring(0, '+', 1, '+', 1);
Factoring::Factoring(int FirstCoefficent = 0, char FirstOperator = '+',int SecondCoefficent = 1, char SecondOperator = '+', int _3rdExpression = 1) : m_FirstCoefficent(FirstCoefficent), m_FirstOperator(FirstOperator),m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),m_3rdExpression(_3rdExpression)
int Factoring::InitialMessage()
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
return 0;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your code suffers from the most vexing parse problem. Use
Factoring Problem1;
.– R Sahu
Aug 12 at 18:14