Can't get this code to compile and work properly
The implementation file:
#include <cmath>
#include "quadEquation.h"
using namespace std;
QuadEquation::QuadEquation()
{
int a,b,c;
}
QuadEquation::QuadEquation(int first, int second, int third)
{
a = first;
b = second;
c = third;
}
int QuadEquation::getA()
{
return a;
}
int QuadEquation::getB()
{
return b;
}
int QuadEquation::getC()
{
return c;
}
int QuadEquation::getDiscriminant()
{
return b * b - 4 * a * c;
}
int QuadEquation::getRoot1()
{
discrim = getDiscrimant();
return -b + sqrt(discrim) / (2 * a);
}
int QuadEquation::getRoot2()
{
discrim = getDiscriminant();
return -b - sqrt(discrim) / (2 * a);
}
The header file:
#ifndef QUADEQUATION_H
#define QUADEQUATION_H
class QuadEquation
{
private:
int a, b, c;
public:
QuadEquation(int, int, int);
int getA();
int getB();
int getC();
int getDiscriminant();
int getRoot1();
int getRoot2();
};
#endif
#include <iostream>
#include "quadEquation.h"
using namespace std;
int main()
{
QuadEquation quad1(1,0,9);
cout << "The first root of the first quadratic equation is: " << quad1.getRoot1() << endl;
return 0;
}
Some errors that i am getting
quadEquation.cpp:5: error: prototype for ‘QuadEquation::QuadEquation()’ does not match any in class `QuadEquation`
quadEquation.h:5: error: candidates are: `QuadEquation::QuadEquation(const QuadEquation&)`
quadEquation.h:10: error: `QuadEquation::QuadEquation(int, int, int)`
quadEquation.cpp: In member function `int QuadEquation::getRoot1()`:
quadEquation.cpp:35: error: `discrim` was not declared in this scope
quadEquation.cpp:35: error: `getDiscrimant` was not declared in this scope
quadEquation.cpp: In member function `int QuadEquation::getRoot2()`:
quadEquation.cpp:40: error: `discrim` was not declared in this scope
One of the constructors you've defined is
QuadEquation::QuadEquation()
{
int a,b,c;
}
But this constructor isn't defined in your header file. Moreover, it looks like this is an error on your part, since this constructor doesn't make much sense - it just declares three local variables and doesn't use any of them. If you do want to declare this constructor, add it to your header file, but judging from your code I don't believe it's necessary.
As to your other errors, look at this code:
int QuadEquation::getRoot1()
{
discrim = getDiscrimant();
return -b + sqrt(discrim) / (2 * a);
}
Two things jump out at me. First, where is discrim declared? Second, if quadratic formulas can have arbitrary complex-valued roots, is there a reason you're returning an int? Is there a different type you could use here instead?
Overall, you should learn to read these compiler error messages. Everything I've pointed out could easily have been gleaned from the error output. Now that you're aware what the problems are, can you see how they generate the given compiler errors?
Hope this helps!
Related
I'm trying to run some test code to learn c++, but I am getting an error telling me the reverseDigits function was not declared in the main.cpp scope:
error: 'reverseDigits' was not declared in this scope.
But the #include "Solutions.h" header was included in main.cpp, so I thought that it would be in scope.
I have checkout other questions, but the answers all relate to problems with circular header file inclusion, which I don't think is the problem here.
Do you know why I am seeing that error?
Solution.h
#ifndef SOLUTION_H
#define SOLUTION_H
class Solution {
public:
Solution();
~Solution();
int reverseDigits(int x);
};
#endif // SOLUTION_H
Solution.cpp
#include "Solution.h"
#include <string>
Solution::Solution()
{
}
Solution::~Solution()
{
}
int Solution::reverseDigits(int x) {
std::string num_string = std::to_string(x);
std::string reversed_num_string {};
for (int i = num_string.length() - 1; i > 0; i--) {
reversed_num_string.push_back(num_string[i]);
}
return stoi(reversed_num_string);
}
main.cpp
#include <iostream>
#include "Solution.h"
int main()
{
int x {123};
int result = reverseDigits(x);
std::cout << result << std::endl;
return 0;
}
You declared reverseDigits as a member function of the Solution class, then defined it without qualifying it as a member of Solution (Edit: You've since changed it to match declaration and definition, but at point of use, you're trying to use an unqualified function, not a member of a Solution object). The declaration in the .h file is visible, but the definition in the .cpp is unrelated, and not visible to main.cpp.
Declare the function outside the class (since it's clearly unrelated to the class), and it should work, changing to:
class Solution {
public:
Solution();
~Solution();
};
int reverseDigits(int x); // NOT inside the class definition
I'll note: I have no idea why you have a Solution class at all. Defining reverseDigits doesn't require it, so I'm not seeing the point. If this is part of some automated evaluation framework, you'll have to give more details
Along with ShadowRanger's valid suggestion, I'll highlight upon how you could have used the data as part of your Solution class and applied the function on it.
Refactoring your class to
class Solution {
public:
Solution(int data);
~Solution();
int reverseDigits();
private:
int m_data;
};
Solution::Solution(int data)
{
m_data = data;
}
Solution::~Solution()
{
}
Even though you could have used std::reverse, fixing the error on the i>=0 is needed to have your own reverse function
int Solution::reverseDigits() {
std::string num_string = std::to_string(m_data);
std::string reversed_num_string {};
for (int i = num_string.length() - 1; i >= 0; i--) {
reversed_num_string.push_back(num_string[i]);
}
return stoi(reversed_num_string);
}
Now call it from your main() as
int main() {
int x = 123;
Solution sol(x);
std::cout << sol.reverseDigits() << std::endl;
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();
}
I stumbled across a strange c++ snippet. I consider this as bad code. Why would someone repeat the function declaration inside a function? It even compiles when changing the type signature to unsigned int sum(int, int) producing the expected result 4294967294j. Why does this even compile?
#include <iostream>
#include <typeinfo>
using namespace std;
int sum(int a, int b){
return a + b;
}
int main()
{
int sum(int, int); // redeclaring sum???
int a = -1;
auto result = sum(a, a);
cout << result << typeid(result).name() << endl;
}
Edit: It compiles for me... but is it valid C++ code? If not why does the compiler (mingw 4.8.1) allow it?
Sometimes there is a sense to redeclare a function inside a block scope. For example if you want to set a default argument. Consider the following code
#include <typeinfo>
using namespace std;
int sum(int a, int b){
return a + b;
}
int main()
{
int sum(int, int = -1 ); // redeclaring sum???
int a = -1;
auto result = sum(a, a);
cout << result << typeid(result).name() << endl;
result = sum(a);
cout << result << typeid(result).name() << endl;
}
Another case is when you want to call a concrete function from a set of overloaded functions. Consider the following example
#include <iostream>
void g( int ) { std::cout << "g( int )" << std::endl; }
void g( short ) { std::cout << "g( short )" << std::endl; }
int main()
{
char c = 'c';
g( c );
{
void g( short );
g( c );
}
}
If that's the actual code, there's no reason to do it.
If the function sum is defined somewhere else though, the declaration inside main makes it accessible only inside main. You can't use it anywhere else in that translation unit (unless of course you declare it). So it's a sort of limiting visibility to where it's needed, but, granted, it's not very readable.
Regarding changing the return type - that's illegal. You're not seeing any issues with unsigned int, but if you try
char sum(int, int); // redeclaring sum???
you'll see there's a problem there.
I realise what this error means, the thing that is puzzling me is why I get it. I have emulated the error in this tiny program for the sake of this question. Here is the code:
source file:
#include "RandomGame.h"
using namespace std;
NumberGame::NumberGame(int i)
{
upperBound = i;
numberChosen = 1 + rand() % upperBound;
}
NumberGame::NumberGame()
{
upperBound = 1000;
numberChosen = 1 + rand() % upperBound;
}
int NumberGame::guessedNumber(int g)
{
if (g == numberChosen)
{
return 2;
}
else if (g < numberChosen)
{
return 0;
}
else
{
return 1;
}
}
header file:
#ifndef NUMBERGAME_H
#define NUMBERGAME_H
#include <iostream>
using namespace std;
class NumberGame
{
private:
int upperBound;
int numberChosen;
int g;
public:
NumberGame(int);
NumberGame();
int guessedNumber(int);
};
#endif
main:
#include <iostream>
#include <cstdlib>
#include "RandomGame.h"
using namespace std;
int makeEven(int);
int main()
{
//only one of these lines will remain uncommented
NumberGame newNumberGame(5); // works
NumberGame newNumberGame; // works
NumberGame newNumberGame(); // doesn't work
// I get the error here, apparently when using empty brackets, newNumberGame
//is not a class anymore. wwwhhyyyyy??????
newNumberGame.guessedNumber(5);
return 0;
}
I haven't done C++ in a while so i'm sure it's something really simple, but why doesn't the empty brackets work? btw it also doesn't work if I just have one constructor with no arguments, or one constructor with a default argument.
This
NumberGame newNumberGame();
declares a function newNumberGame() taking no arguments and returning a NumberGame.
C++'s grammar is grown over several decades, rather than being designed in a single act of creation. Consequently, it has many ambiguities that needed to be resolved. This ambiguity was resolved for a b() to be a function declaration rather than an object definition.
I am making a new program in c++ i get the current error
expected primary-expression before ‘int’
about this line
p1::pascals_triangle(int depth);
My code is:
this is my functions.cpp
using namespace std;
/**
* Note the namespace qualifier used here - I did not bring 'p1' in
* scope, therefore we need the "p1::" to preceed the function definitions.
*/
void p1::pascals_triangle(int depth) {
// Implement problem 1 in here.
this is main
using namespace std;
int main(int argc, char *argv[]) {
// Need to implement argument processing here
// If you want to set a function pointer, you just take the
// address of the function ('&' means 'take address of'):
double (*pf)(double k); // declares the function pointer
pf = &p1::test_function;//test_function; // sets the value of 'pf' to the address of the 'p1::test_function' functio\
n.
p1::pascals_triangle(int depth);
Unless you're declaring a method, you probably don't need the keyword "int".
#include <iostream>
namespace foo {
void pascals_triangle(int depth) {
std::cout << depth << std::endl;
}
int another_method(int y);
}
using namespace std;
int
foo::another_method(int y) {
cout << "called another_method with " << y << endl;
return 8;
}
int main(void) {
int x = 5;
foo::pascals_triangle(x);
foo::another_method(x + 1);
return 0;
}
If I were to write instead:
int main(void) {
int x = 5;
foo::pascals_triangle(int x);
foo::another_method(x + 1);
return 0;
}
I'd get:
In function ‘int main()’:
error: expected primary-expression before ‘int’
p1 needs to be an existing namespace or class name.
If that does not solve the problem you will have to give some surrounding code to make sense of your question ;)
Good luck.