【问题标题】:vector<wstring> as Return value and Parametervector<wstring> 作为返回值和参数
【发布时间】:2012-10-26 22:04:22
【问题描述】:

我想为我的程序创建一些模块。我想调用一个函数并将一个向量作为参数传递。返回值也应该是一个向量。

我的代码是这样的

main.cpp
//BlueSmart.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
#include "stdafx.h"

#define WIN32_LEAN_AND_MEAN

using namespace std;

#pragma comment(lib, "Irprops.lib")



BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {
sizeof(BLUETOOTH_FIND_RADIO_PARAMS)
};

BLUETOOTH_RADIO_INFO m_bt_info = {
sizeof(BLUETOOTH_RADIO_INFO),
0,
};

BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
1,
0,
1,
1,
1,
15,
NULL
};

BLUETOOTH_DEVICE_INFO m_device_info = {
sizeof(BLUETOOTH_DEVICE_INFO),
0,
};

HANDLE m_radio = NULL;
HBLUETOOTH_RADIO_FIND m_bt = NULL;
HBLUETOOTH_DEVICE_FIND m_bt_dev = NULL;

int wmain(int argc, wchar_t **args) {

while(true) {
m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio);

do {
  localBluetoothDevices ();

  m_search_params.hRadio = m_radio;
  ::ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
  m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
  m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);

  vector<wstring> vec;
  int m_device_id = 0;
  do {
   wostringstream tmp;
   ++m_device_id;
   //Something like this <----------------------------------------
   externBluetoothDevices (vec);
   //Something like this <----------------------------------------

      wprintf(L"********************************************************************** \n");
   wprintf(L"\tDevice %d:\r\n", m_device_id);
   wprintf(L"\t\tName: %s\r\n", m_device_info.szName);
   wprintf(L"\t\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5]);
   wprintf(L"====================================================================== \n");


   for (int i = 0; i < 6; i++) {
    tmp << hex << m_device_info.Address.rgBytes [i];
    if (i < 5)
     tmp << L':';
   }

   vec.push_back(tmp.str());

  } while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));


  BluetoothFindDeviceClose(m_bt_dev);
  //Sleep(10*1000*60);
  Sleep(10000);

} while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));
BluetoothFindRadioClose(m_bt);
}
return 0;
}

//Lokal verfügbare bzw. angeschlossene Bluetooth-Devices
void localBluetoothDevices (){
  int m_radio_id = 0;
  m_radio_id++;
  BluetoothGetRadioInfo(m_radio, &m_bt_info);      
  //Lokaler Bluetoothadapter
  wprintf(L"====================================================================== \n");
  wprintf(L"Local Device Nr. %d\n", m_radio_id);
  wprintf(L"\tName: %s\r\n", m_bt_info.szName);
  wprintf(L"\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n",      m_bt_info.address.rgBytes[0], m_bt_info.address.rgBytes[1], m_bt_info.address.rgBytes[2], m_bt_info.address.rgBytes[3], m_bt_info.address.rgBytes[4], m_bt_info.address.rgBytes[5]);

 }

//Extern verfügbare bzw. Bluetooth-Devices
 vector<wstring> externBluetoothDevices (vector<wstring> &vec){
return vec;
 }

stdafx.h

 #pragma once

 #include "targetver.h"

 #include <stdio.h>
 #include <tchar.h>
 #include <winsock2.h>
 #include <windows.h>
 #include <stdlib.h>
 #include <bthdef.h>
 #include <BluetoothAPIs.h>
 #include <iostream>
 #include <vector>
 #include <algorithm>
 #include <string>
 #include <sstream>
 #include <iomanip>
 #include <conio.h>

 void localBluetoothDevices ();
 vector<wstring> externBluetoothDevices (vector<wstring>);

它说向量不是已知类型。我做错了什么?

【问题讨论】:

  • 您是否包含了 ?你说的是 using namespace std;或类似的?你真的把你的函数叫做外部吗?在询问编译器错误消息以发布实际代码时总是一个好主意,而不是错误的摘录。并说出哪一行有错误。
  • extern 是 C++ 保留关键字,不能使用,请see here 列出。
  • @user1557170 仍然不是您的真实代码。发布代码,而不是示例,而不是 ...,您可以在计算机屏幕上看到的真实实际代码,但我们不能。
  • 实际上,header 文件中的using namespace std; 是一个非常糟糕的主意。我至少会在头文件中推荐std::vector
  • 太棒了。更多代码。现在把stdafx.h放到帖子里,我们终于给你答案了。

标签: c++ function vector


【解决方案1】:

在 stdafx.h 中替换

vector<wstring> externBluetoothDevices (vector<wstring>);

std::vector<std::wstring> externBluetoothDevices (std::vector<std::wstring>);

基本上问题在于,尽管您将 using namespace std; 放在了 cpp 文件中,但在看到 using 声明之前,该文件不计入头文件中。

另请注意,您在 cpp 文件中的定义是不同的。在cpp文件中你有一个参考

vector<wstring> externBluetoothDevices (vector<wstring>&);

决定你真正想要的。

【讨论】:

  • 为什么要用 std:: 替换?!
  • 班级的真实姓名是std::vectorusing namespace std; 允许您错过 std:: 部分。但是你没有`using namespace std;'在您的头文件中,它只会在以后出现。不要认为这意味着你应该把 'using namespace std;但是,在您的头文件中,这是一个坏主意。
  • 你知道我为什么会收到一些编译指示警告吗?!
  • @user1557170 必须看到警告,但这是另一个问题。
  • 对不起,没有。 (特别是因为你没有说出警告是什么)。
【解决方案2】:

你应该传递一个向量的指针。

【讨论】:

  • 这如何回答这个问题?
猜你喜欢
  • 2010-10-14
  • 2011-08-30
  • 2011-08-16
  • 2016-08-17
  • 1970-01-01
  • 2012-06-30
  • 2014-03-10
  • 2013-07-28
  • 1970-01-01
相关资源
最近更新 更多