這是我的 CPP 代碼:
#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;
}
這是我收到的錯誤:
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 ==========
請你能幫我解決CPP Masters嗎?
uj5u.com熱心網友回復:
帶著目標開始是有益的。你想做什么不是很清楚。
int func3(int *p, int index)是函式,int func3是變數。這就是你的錯誤。您可以重命名變數以避免此錯誤。
此外,這不是呼叫函式的正確方法: int func3(currentprice);. 首先,該函式采用兩個引數而不是一個引數。其次,回傳值不會存盤在任何地方。
此外,控制臺程式將從該main函式開始執行。所以manin應該是main。
uj5u.com熱心網友回復:
這個 ...
int func3;
... 是func3作為型別變數的宣告int。它隱藏了 function 的檔案范圍宣告func3()。
這個 ...
int func3(currentprice);
...也是func3由于int前面的原因(不是函式呼叫)的宣告。您不能func3在同一范圍內重新宣告區域變數,即使這兩個宣告指定了相同的型別。
int func3;徹底洗掉。也洗掉intfrom int func3(currentprice);。
uj5u.com熱心網友回復:
@JohnBollinger @DKS @JohnnyMopp @DrewDormann @ThomasMatthews 伙計們,我確實按照您的建議修復了它,結果成功了。但是,它應該需要 3 cin(p1,p2,p3) 并且應該計算當前價格,而是顯示空控制臺。我正在添加以下新代碼:
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/338846.html
上一篇:如何在遞回函式中保持值不變?
下一篇:Foreach回圈以檢查值
