[英]Syntax trouble with inline access specifiers
I am very new to object oriented programming in c++ and I am having some difficulty with syntax when access specifiers are used inline with variables, classes, or functions.我对 C++ 中的面向对象编程非常陌生,当访问说明符与变量、类或函数一起使用时,我在语法方面遇到了一些困难。 The code below is not mine.
下面的代码不是我的。 It is from this post.
它来自这个帖子。 Judging by the popularity of the thread I'm sure others got it to compile.
从线程的流行程度来看,我确信其他人已经编译了它。 However I am having difficulty.
但是我有困难。
public class CartEntry
{
public float Price;
public int Quantity;
}
public class CartContents
{
public CartEntry[] items;
}
public class Order
{
private CartContents cart;
private float salesTax;
public Order(CartContents cart, float salesTax)
{
this.cart = cart;
this.salesTax = salesTax;
}
public float OrderTotal()
{
float cartTotal = 0;
for (int i = 0; i < cart.items.Length; i++)
{
cartTotal += cart.items[i].Price * cart.items[i].Quantity;
}
cartTotal += cartTotal*salesTax;
return cartTotal;
}
}
I tried a simple class to identify the problem我尝试了一个简单的类来识别问题
This compiles:这编译:
#include <iostream>
class C{
int i;
};
int main(){}
This does not:这不会:
#include <iostream>
class C{
public int i;
};
int main(){}
nor does this:这也不是:
#include <iostream>
public class C{
int i;
};
int main(){}
This does:这样做:
#include <iostream>
class C{
public: int i;
};
int main(){}
This does not:这不会:
#include <iostream>
public: class C{
int i;
};
int main(){}
I am using the GNU c++ compiler V6.3.1 without any build flags.我正在使用没有任何构建标志的 GNU c++ 编译器 V6.3.1。 If this syntax is incorrect, why do I see it demonstrated so often.
如果此语法不正确,为什么我经常看到它的演示。 If it is correct, am I missing a compiler option, header file, or something else?
如果正确,我是否缺少编译器选项、头文件或其他内容?
The answer is simply that your example post discusses Java instead of C++.答案很简单,您的示例帖子讨论的是 Java 而不是 C++。 The syntax for access specifiers and this pointers (as well as many other items) is different.
访问说明符和this指针(以及许多其他项)的语法是不同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.