c++ Xcode 预期为“(”用于函数式强制转换或类型构造
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26437103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ Xcode expected '(' for function-style cast or type construction
提问by Meeeeee
I'm trying to compile this cpp and h files but it keeps giving me this error "expected '(' for function-style cast or type construction" and it points to the constructor
我正在尝试编译这个 cpp 和 h 文件,但它一直给我这个错误“预期的 '(' 用于函数式强制转换或类型构造”,它指向构造函数
GasTank::GasTank(double a){
capacity=a;
}
Any thoughts why? I can't figure it out why it is giving me that error. Here is the rest of the code:
任何想法为什么?我不明白为什么它会给我这个错误。下面是代码的其余部分:
//
// Header.h
// labs
//
// Created by Pxndroid on 10/17/14.
// Copyright (c) 2014 Pxndroid. All rights reserved.
//
#include<string>
using namespace std;
class GasTank
{
private:
double amount;
double capacity;
public:
GasTank(double a);
void addGas(double b);
void useGas(double c);
bool isEmpty();
bool isFull();
double getGasLevel();
double fillUp();
};
and:
和:
//
// main.cpp
// labs
//
// Created by Pxndroid on 10/17/14.
// Copyright (c) 2014 Pxndroid. All rights reserved.
//
#include <iostream>
#include "Header.h"
using namespace std;
int main()
{
GasTank::GasTank(double a){
capacity=a;
}
void GasTank::addGas(double b){
if((amount+b)>capacity){
amount=capacity;
}
else{
amount+=b;
}
}
void GasTank::useGas(double c){
if((amount-c)<0){
amount=0;
}
else{
amount-=c;
}
}
bool GasTank::isEmpty(){
if(amount<0.1){
return true;
}
else{
return false;
}
}
bool GasTank::isFull(){
if(amount>capacity-0.1){
return true;
}
else{
return false;
}
}
double GasTank::getGasLevel(){
return amount;
}
double GasTank::fillUp(){
capacity-=amount;
amount+=capacity;
return capacity;
}
}
回答by cdhowie
Move the definitions of the GasTank
members outside of int main()
. They don't belong there, and the compiler is not expecting them; they are not part of the main()
function.
将GasTank
成员的定义移到 之外int main()
。它们不属于那里,编译器也不期待它们;它们不是main()
函数的一部分。