【发布时间】:2015-02-19 19:28:46
【问题描述】:
我正在尝试创建一个程序,该程序要求用户输入一个数字并生成斐波那契数。 Main 获取数字并将该数字设置为向量的第一个值。然后向量将被发送到一个线程以生成等于该数字的斐波那契数(因此,如果我输入3,该线程将生成前 3 个斐波那契数并将其添加到向量中)。然后向量将返回给主函数,以便它可以打印出值。
这是我所拥有的,但它一直在说 Error: expression must be a pointer to a complete object type 我尝试打印出向量的第一个值。
#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
/* This function is executed by the child thread */
DWORD WINAPI calcFibNumbers(LPVOID fibNumber)
{
cout << fibNumber[0];
return 0;
}
/* This is the main function and start of program */
int main()
{
//Local Variables
vector<int> fibNumbers;
int numOfFib;
//Create child thread
HANDLE childThread = CreateThread(NULL, 0, calcFibNumbers, &fibNumbers, CREATE_SUSPENDED, NULL);
//Ask user for the number of fibonacci numbers
cout << "Enter how many fibonacci numbers you would like the program to generate: ";
cin >> numOfFib;
fibNumbers.push_back(numOfFib);
ResumeThread(childThread);
if (childThread)
{
WaitForSingleObject(childThread, INFINITE);
}
getchar();
getchar();
return 0;
}
谁能指出我正确的方向?
【问题讨论】:
-
这不是 C++,std::thread 是 C++
-
@DieterLücking 语言是 C++,但我使用 Win Thread Library 进行多线程处理
-
您需要将 LPVOID fibNumber 转换回向量
* in calcFibNumbers -
@TonyJiang 我添加了这个
vector<int>fibNum = (vector<int>*)fibNumber; cout << fibNum[0];,但我仍然收到错误:IntelliSense: no suitable constructor exists to convert from "std::vector<int, std::allocator<int>> *" to "std::vector<int, std::allocator<int>>
标签: windows multithreading vector