// 全局函数作变元.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> using namespace std; #include <string> class Building { friend void visit1(Building* building); public: string m_sittingroom; private: string m_bedroom; public: Building() { string m_sittingroom = "客厅"; string m_bedroom = "卧室"; } }; void visit(Building* building) { cout << "正在访问" << building->m_sittingroom << endl; } void visit1(Building* building) { cout << "正在访问" << building->m_bedroom << endl; } ////{ // cout<< //} int main() { Building b1; visit(&b1); visit1(&b1); }为什么输出不对
时间: 2024-04-26 08:25:12 浏览: 20
在类的构造函数中,你使用了 `string` 类型的局部变量 `m_sittingroom` 和 `m_bedroom`,而没有使用类中定义的成员变量。因此在构造函数中初始化的是局部变量,而成员变量的值没有被修改。
所以,当你在 `visit` 函数中访问 `m_sittingroom` 成员变量时,它的值是未被初始化的。同样地,在 `visit1` 函数中访问 `m_bedroom` 成员变量时,由于此成员是私有的,因此不能从友元函数中访问。因此,你需要将 `visit1` 函数中的访问方式改为公有成员函数或者使用类的其他友元函数来访问。
相关问题
/home/vv/untitled5/main.cpp:6: error: undefined reference to `vtable for WorkerThread'
这个错误通常是由于在使用自定义的线程类时,没有正确声明和定义信号槽的原因导致的。
要解决这个错误,你需要确保在自定义的`WorkerThread`类中正确声明了信号和槽,并在相应的地方进行了定义。在你的代码中,你可能需要添加一个信号或槽的声明,并在类的实现部分进行定义。
以下是一个示例代码,展示了如何在自定义线程类中声明和定义信号槽:
```cpp
#include <QThread>
#include <QDebug>
// 自定义的工作线程类
class WorkerThread : public QThread
{
Q_OBJECT
public:
// 声明一个自定义的信号
void mySignal();
protected:
// 重写run()函数来执行线程任务
void run() override;
private slots:
// 声明一个槽函数
void mySlot();
};
// 定义信号的实现
void WorkerThread::mySignal()
{
qDebug() << "Signal emitted";
}
// 定义槽函数的实现
void WorkerThread::mySlot()
{
qDebug() << "Slot called";
}
// 定义run()函数的实现
void WorkerThread::run()
{
// 执行线程任务
}
```
请注意,为了正确使用信号槽机制,`WorkerThread`类需要使用`Q_OBJECT`宏进行标记,并且在类声明后面添加`#include "main.moc"`以便使用元对象编译器(moc)进行处理。
如果你的代码中已经正确声明和定义了信号和槽,那么请确保`.pro`文件中添加了`QT += core`来链接Qt核心模块。
希望这可以帮助你解决问题!如果还有其他疑问,请随时提问。
// MyDriverC.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <stdlib.h> #include <stdio.h> #include <windows.h> using namespace std; int main() { cout << "Hello World!\n"; HANDLE hdevice = NULL; hdevice = CreateFile(L"\\\\.\\MyFirstDevice", GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hdevice == INVALID_HANDLE_VALUE) { cout << "Oped device filed" << endl; system("pause"); return 0; } cout << "Oped device successed" << endl; CloseHandle(hdevice); system("pause"); return 0; } 无法运行
这段代码存在两个问题:
1. 缺少头文件
需要添加以下头文件:
```C++
#include <tchar.h>
```
这个头文件中定义了_TCHAR类型,以及相关的宏定义和函数。
2. 路径名错误
在Windows中,需要注意路径名中的反斜杠。在C/C++中,反斜杠是一个特殊的字符,用来表示转义序列。因此,在路径名中使用反斜杠时需要进行转义,即使用双反斜杠"\\\\"代替单个反斜杠"\"。因此,正确的代码应该是:
```C++
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <tchar.h> // 添加头文件
using namespace std;
int main()
{
cout << "Hello World!\n";
HANDLE hdevice = NULL;
hdevice = CreateFile(_T("\\\\.\\MyFirstDevice"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); // 修改路径名
if (hdevice == INVALID_HANDLE_VALUE)
{
cout << "Oped device filed" << endl;
system("pause");
return 0;
}
cout << "Oped device successed" << endl;
CloseHandle(hdevice);
system("pause");
return 0;
}
```
通过添加头文件和修改路径名,应该可以解决无法运行的问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)