- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我定义了一个简单的类。定义在头文件中,实现在cpp文件中:
标题:
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point(int x, int y);
int getX();
int getY();
private:
int x, y;
};
#endif
CPP
#include Point.h
Point::Point(int x=2, int y=2) {
this->x = x;
this->y = y;
}
int Point::getX() {
return x;
}
int Point::getY() {
return y;
}
这不是编译,我不确定为什么。另外,当我想实例化一个点时在其他地方,比如 main.cpp,我在顶部 #include 是什么,只是 Point.h?如果是这样,它怎么知道要查看 Point.cpp?
最佳答案
#include Point.h
Point.h
需要引号。
Point::Point(int x=2, int y=2)
默认参数应该在头文件的声明中,而不是在 .cpp 文件的定义中(它将按原样编译,但不会执行您想要的操作)。
在文体上,将局部变量或参数命名为与成员变量相同的名称通常不是一个好主意:这样做很容易导致混淆。您还可以使用初始化列表初始化成员变量:
Point::Point(int x_, int y_) : x(x_), y(y_) { }
由于您当前编写的代码,x
和y
均已构造,然后您将其赋值给它们。使用初始化列表消除了分配并直接构造它们。这对于 int
等基本类型无关紧要,但对于类类型却很重要。
This is not compiling and I am not sure why.
通常编译器发出的错误信息是有帮助的。如果不出意外,如果它对您没有帮助,那么它对我们这些试图提供帮助的人很有帮助。
Also, when I want to instantiate a point somewhere else, say main.cpp, what do I
#include
at the top, just Point.h?
是的。
How does it know to look in Point.cpp?
您编写的每个 .cpp 文件都会编译成一个单独的目标文件。一旦所有 .cpp 文件都编译成目标文件,目标文件就会全部链接在一起成为一个可执行文件(或库)。链接器找出所有内容的定义位置。
关于c++ - 头文件。 ETC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5422665/
如果不是,android如何判断用户是否属于某个组? 最佳答案 此线程讨论了如何完成:http://groups.google.com/group/android-ndk/browse_thread/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我正在尝试根据此 SO 查找文件 my.cnf邮政。有了答案之一,我在 ssh 到远程服务器后输入了 mysqladmin --help 。 部分输出: Default options are rea
我有cloudera 5.7,我也有Cloudera Manager。 在目录/etc/hadoop下,我看到了三个子目录。 /etc/hadoop/conf /etc/hadoop/conf.clo
我想在我的A.cpp文件中添加一个#include指令,而头文件B.h在两层文件夹中up(比如源文件在*E:\A\B\C\D\E\F\G\H*,头文件在*E:\A\B\C\F*,当然,名称会比这个长很
我是 Linux 新手。我正在浏览 Linux 中的文件系统。尽管 Google 中有许多链接说明/etc/init 和/etc/inittab 之间的区别,但我真的对两者感到困惑。谁能给我一个明确的
文件夹系统中有两个相似的文件夹,文件应该配置在哪个文件夹中?有什么区别? 最佳答案 没有什么区别。从 OX X 10.2 开始,/etc/* 是到/private/etc/* 的符号链接(symbol
要自动挂载,管理员可以编辑/etc/fstab 或/etc/mtab。两者有什么区别?如果我在两者中设置相同的 mount 项,哪一个生效? 在/etc/stab 中 共享/media/sf_shar
我需要将基于 dnsmasq 的 DHCP 服务器配置转换为 ISC dhcpd,因此有必要将大量固定 IP 地址转换为新格式。 输入格式为: 84:2b:2b:19:05:a7 192.168.14
场景1: 我有 2 个用户,每个用户都有不同的主要组。对于 user1,主要组是 GID 501 的 group1。对于 user2,主要组是 GID 502 的 group2。 我编辑了 /etc/
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 此问题似乎与 a specific programming problem, a sof
我需要将特定的传出连接(从我系统上的任何 网络客户端)重定向到特定的 IP。是的,可以通过在 /etc/hosts 文件中添加这一行来完成: 123.456.789.012 www.mydomain
需要用一个已知的主机名grep/etc/hosts,然后从/etc/hosts中抓取该主机名的ip地址。 我不是程序员,不知道该怎么做。我对正则表达式的经验非常有限,但认为这可能会以某种方式起作用。我
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我一直在研究一个小的 Java 问题集,并遇到了一些麻烦。我在编写正则表达式方面不太有经验,并且确实可以使用两个来验证 Java 中的/etc/group 和/etc/passwd 中的行条目。 我找
代替 HTML JavaScript var username = document.getElementById('username'); username.classList.add('cla
我正在尝试 grep /etc/passwd 和 /etc/group 列出所有用户和用户所属的每个组. 即: Jon Doe, root:randomgroup:randomgroup2:rando
我需要使用 bash 找出我正在运行的 Linux 发行版。找到this page ,这非常有帮助。 但是我的系统有两个/etc/*-release 文件 /etc/lsb-release /etc/
如何保护/etc/passwd 和/etc/shadow 不被并发访问?我在 pwd.h 中没有看到任何锁定机制。我看到 pwd_mkdb 的手册提到获取锁,但它只是锁定文件以进行独占访问吗? 如果我
经过一番挖掘后,我已经确定 - 我可能错了,因为我对这些问题的了解相当粗略 -/etc/init.d 中的脚本按照/etc/rcN.d 中的符号链接(symbolic link)确定的顺序运行. 我不
我是一名优秀的程序员,十分优秀!