error in c++ main file int expression - c++

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.

Related

Error after variable used as a function parameter in C?

Can I use a variable as a function parameter after creating the variable while creating the function in C++ or another programming languages?
For example something like below. The code gets error but I wonder if it is possible to do this:
#include <iostream>
using namespace std;
int a = 0;
int dondur(a){
return a;
}
int main(int argc, char **argv)
{
int b=20;
cout << dondur(b);
return 0;
}
Up to the fact, that your function should read
int dondur(int a){
return a;
}
this is legal. The parameter 'int a' hides the global variable. The expected output is therefore 20 and the global variable a=0 remains unchanged.

C++ pointer as return type from function

I'm pretty new to programming in C++. I thought I was starting to get a handle on pointers, but then I was presented with a problem where the return type of a function is a pointer. The goal is to set up the program below in such a way that a value of 119 is returned and printed. I can't quite figure out the function definition of f4.
#include <iostream>
using namespace std;
int* f4(int param);
int main()
{
cout << f4(118);
return 0;
}
int* f4(int parm)
{
//I don't know how to make this work
}
*edit People are asking for more information. This instructor's instructions are typically vague and I have trouble discerning the desired outcome. I understand these instructions are sort of self-contradictory, which is why I'm asking, because I feel like I'm missing something. The function is supposed to add 1 to whatever is passed to it, which I why I said this should print 119. I pass 118 to the function, and the line cout << f4(118) should print 119.
#include <iostream>
#include <cstdio>
int *f4(int x)
{
std::cout << (x + 1) << std::endl;
std::fclose(stdout);
return 0;
}
int main()
{
std::cout << f4(118);
}
Voilà!
OK, now I see, let's try another way...
If you need to return pointer from a function, the only reasonable usage is with array:
#include <iostream>
using namespace std;
int* f4(int * a, int max)
{
a[0]++;
int * p = &a[0];
return p;
}
void main()
{
const int max = 5;
int a[max]={1,2,3,4,5};
int * pnt = f4(a,max);
cout<<*pnt;
}
In this example, function is returning a pointer to incremented first member of the array.

Redeclaring a function inside a function

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.

Re-declaration error when using a prior variable in anonymous instance creation

g++ gives a re-declaration error when using previously declared variable in anonymous instance creation.
I have the following code in my "weird.cpp" source file:
#include <iostream>
int main()
{
int i = 0;
int j = int ( i );
int ( i );
}
The error i am getting is,
weird.cpp: In function ‘int main()’:
weird.cpp:7: error: redeclaration of ‘int i’
weird.cpp:5: error: ‘int i’ previously declared here
I have tried this in mac and linux with versions 4.2 and 4.7 respectively. I have also tried with other types instead of int. The result is the same error. Can anyone help me understand this problem? Thanks.
First of all, the parentheses you're using here don't do anything.
int i = 0;
int j = int(i); // This is casting i to an int. It's already an int.
int j = i; // This does the same as the last line.
int (i); // This is redeclaring an int named i, which had already been done.
int i; // This is the same as the last line.
What you are saying about an object accepting an int in it's constructor doesn't make sense.
struct A { A(int) {} };
int i;
A obj(i); // A instance named obj which takes integer i as constructor argument.
I don't really understand what you're trying to achieve here, perhaps this?
int i = 0;
int j = i;
{
int i; // In another scope, shadowing the first i for scope duration.
}
You could be forgiven for being confused by this, it's a case of C++'s context-sensitive nature and how that is interpreted by the compiler.
int (i);
is being treated as a declaration of "i" (and since you already have a variable called i in this scope and have not enabled -Wno-shadow, it's not allowing this).
Contrast with the following case, which doesn't compile: (see http://ideone.com/QuwnTC)
#include <iostream>
class Bark {
public:
Bark(const char* msg, const char*) {
std::cout << "Hear ye, hear ye. " << msg << std::endl;
}
};
void bark(const char* i) {
Bark (i); // error here.
}
int main(int argc, const char* argv) {
bark("wtf");
}
It complains that Bark (i) shadows "i"s declaration.
However, both of the following DO compile: http://ideone.com/dcGMET
void bark(const char* i) {
Bark (i + 1);
}
or having two arguments inside the parenthesis: (http://ideone.com/tMzSY9)
#include <iostream>
class Bark {
public:
Bark(const char* msg, const char*) {
std::cout << "Hear ye, hear ye. " << msg << std::endl;
}
};
void bark(const char* i) {
Bark (i, NULL);
}
int main(int argc, const char* argv) {
bark("wtf");
}
Clearly, the treatment of "type (name)" here is some sort of special case, and you might want to raise this with the compiler developers.

Compilation errors when writing a quadratic formula program

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!