Home > Software engineering >  C function declaration problem, error C2086. Help me CPP Masters
C function declaration problem, error C2086. Help me CPP Masters

Time:10-28

This is my CPP code:

#include <iostream>
#include <string>
#include <array>
#include <cmath>
using namespace std;
/*
bool func1(int budget){
    if( budget < 0 ) {
        cout << "Budget cannot be negative."<< endl; 
        return false; 
    }
    else {
        return true; 
    }
}
bool func2(int num1, int num2, int num3){ 
    if (int num1 < 0 || int num2 < 0 || int num3 < 0 ) {
        cout << "All quantities must be positive."<< endl;
        return false;
    }
    else { 
        return true;
    }
}
*/
int func3(int *p, int index){
    
    int currentprice = *(p) * 5   *(p 1) * 10   *(p 2) * 15  ;
    return currentprice; 

}

int manin() {
    int budget;
    int num1, num2, num3;
    int p1;
    int p2;
    int p3;
    int currentprice;
    int func3;
    cin >> p1 >> p2 >> p3 ;
    int position[3] ;
    position[0] = p1 ; 
    position[1] = p2 ; 
    position[2] = p3 ; 
    int *p;
    p= position;
    int func3(currentprice);
    cout << currentprice;
return 0; 
}

This is the error I receive :

1>------ Build started: Project: ConsoleApplication3, Configuration: Debug Win32 ------
1>  Source.cpp
1>c:\users\noname\onedrive\masaüstü\cs\consoleapplication3\consoleapplication3\source.cpp(56): error C2086: 'int func3' : redefinition
1>          c:\users\noname\onedrive\masaüstü\cs\consoleapplication3\consoleapplication3\source.cpp(48) : see declaration of 'func3'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

please can you help me out CPP Masters?

CodePudding user response:

It's beneficial to start with a goal in mind. What are you trying to do is not very clear.

int func3(int *p, int index) is function and int func3 is a variable. That is what your error is about. You can rename the variable to avoid this error.

Also, this is not the correct way to call a function: int func3(currentprice);. First that function takes two parameters instead of one. Second, the return value is not stored anywhere.

Also, a console program will start executing from the main function. So manin should probably be main.

CodePudding user response:

This ...

    int func3;

... is a declaration of func3 as a variable of type int. It shadows the file-scope declaration of function func3().

This ...

    int func3(currentprice);

... is also a declaration of func3 (not a function call) on account of the int in front. You may not redeclare local variable func3 in the same scope, not even if the two declarations designate the same type.

Remove the int func3; altogether. Also remove the int from int func3(currentprice);.

CodePudding user response:

@JohnBollinger @DKS @JohnnyMopp @DrewDormann @ThomasMatthews Guys, I did fix it with your suggestions it results in success. However, it should take 3 cin(p1,p2,p3) and should cout currentprice İnstead it shows empty consol. I am adding the new codes below:

#include <iostream>
#include <string>
#include <array>
#include <cmath>
using namespace std;
/*
bool func1(int budget){
    if( budget < 0 ) {
        cout << "Budget cannot be negative."<< endl; 
        return false; 
    }
    else {
        return true; 
    }
}
bool func2(int num1, int num2, int num3){ 
    if (int num1 < 0 || int num2 < 0 || int num3 < 0 ) {
        cout << "All quantities must be positive."<< endl;
        return false;
    }
    else { 
        return true;
    }
}
*/
int func3(int *p){
    
    int currentprice = *(p) * 5   *(p 1) * 10   *(p 2) * 15  ;
    return  currentprice; 

}









int main() {
    /*
    int budget;
    int num1, num2, num3;
    */
    int p1;
    int p2;
    int p3;
    int currentprice= 0;
    cin >> p1 >> p2 >> p3 ;
    int position[3] ;
    position[0] = p1 ; 
    position[1] = p2 ; 
    position[2] = p3 ; 
    int *p;
    p= position;
    func3(p);
    cout << currentprice;
return 0; 
}
  • Related