没有合适的构造函数存在 - 矢量图
Posted
技术标签:
【中文标题】没有合适的构造函数存在 - 矢量图【英文标题】:No Suitable Constructor Exists - Vector 【发布时间】:2021-08-04 09:31:08 【问题描述】:我今天为自己开始了一个简单的编码项目,但我无法弄清楚这个错误或如何解决它。我得到的错误发生在我的do
循环中。更具体地说,它发生在每个将Nums
作为参数传递的函数调用中。错误状态no suitable constructor exists to convert from to "std::vector<int, std::allocator<int>> [10]" to "std::vector<int, std::allocator<int>>"
对此的任何帮助将不胜感激!蒂亚!
//This program checks to see if a multiplied vector value is odd or even
//CheckOddEven.cpp
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int setSize(int size, vector <int> Nums) //Sets vector size to user defined size
//Prompt user to enter an integer to establish array size
cout << "How many integers would you like to multiply?: ";
//Store user input to size
cin >> size;
//Resize vector to user size
Nums.resize(size);
return size;
vector <int> setNum(int size, int x, vector <int> Nums) //Sets values in each index
//Insertion loop to Nums vector
for (int i = 0; i < size; i++)
cout << "Enter integer #" << i + 1 << ": ";
cin >> x;
Nums.push_back(x); //Assigns value into vector
return Nums;
int doMath(vector <int> Nums, int result)
//Loop through Nums vector and multiply all values
accumulate(Nums.begin(), Nums.end(), result, multiplies<int>());
return result;
string oddEven(int result, string msg) //Check if int is even by seeing if there is a remainder
//If no remainder exists then positive
if (result % 2 == 0)
msg += "positive.";
//If remainder exists then negative
else
msg += "negative.";
return msg;
void Display(int result, string msg)
cout << "The product of your integers is " << result << endl;
cout << msg;
char checkCont(char cont)
//Asks user if they want to check another integer
cout << "Would you like to check another? (y/n)" << endl;
//Assigns input as char into cont
cin >> cont;
//Displays exit message and ends do-while
if (cont == 'n' || cont == 'N')
cout << "Thank you for using the Odd-Even Checker!" << endl;
cout << "Have a great day!" << endl;
return 'z';
int main()
int size; //User defined vector size
string msg = "The number you calculated is ";
char cont = ' '; //Char value for checking another vector
vector <int> Nums[10]; //Create int vector with predetermined size..can be changed with resize
int x; //User numbers
int result;
cout << "Welcome to the Odd-Even Checker!" << endl;
cout << "This program multiplies a given amount of numbers, then checks to see if the answer is even or odd." << endl;
do //Requires to run at least once
setSize(size, Nums);
setNum(size, x, Nums);
doMath(Nums, result);
oddEven(result, msg);
Display(result, msg);
checkCont(cont);
while (cont == 'y' || cont == 'Y'); //end do-while
return 0;
【问题讨论】:
这不是 C。不要滥用标签。 【参考方案1】:你声明了一个向量数组
vector <int> Nums[10];
但是将此数组传递给需要向量类型的标量对象的函数
int setSize(int size, vector <int> Nums);
//...
setSize(size, Nums);
也许你指的是一个包含 10 个元素的向量而不是数组
vector <int> Nums(10);
或者只是一个空向量
vector <int> Nums;
似乎还有一个问题是您没有通过引用传递向量。例如,函数setSize
处理传递的向量的副本。
因此函数内的这条语句
Nums.resize(size);
没有意义。
将函数中需要向量函数参数的类型更改为引用类型,如
int setSize(int size, vector <int> &Nums);
^^^^^^
注意参数size的值没有在函数内部使用,所以参数size
没有意义。
这个函数也没有意义
int doMath(vector <int> Nums, int result)
//Loop through Nums vector and multiply all values
accumulate(Nums.begin(), Nums.end(), result, multiplies<int>());
return result;
调用算法std::accumulate
后变量结果没有改变。
您还传递了一个未初始化的变量结果
int result;
你至少应该写
long long int doMath( const std::vector<int> &Nums )
//Loop through Nums vector and multiply all values
long long result = std::accumulate(Nums.cbegin(), Nums.cend(), 1ll, std::multiplies<long long>());
return result;
您应该将您的程序拆分成小的子程序并分别测试每个使用的功能,因为您似乎不明白自己在做什么。之后,您可以将所有子程序组合在一个程序中。
例如要测试函数setSize
,你可以写一个像这样的简单程序
#include <iostream>
#include <vector>
std::vector<int>::size_type setSize( std::vector<int> &Nums )
//Prompt user to enter an integer to establish array size
std::cout << "How many integers would you like to multiply?: ";
//Store user input to size
std::vector<int>::size_type size = 0;
std::cin >> size;
//Resize vector to user size
Nums.resize( size );
return size;
int main()
std::vector<int> Nums;
auto size = setSize( Nums );
if ( size == Nums.size() )
std::cout << "The function works correctly.\n";
else
std::cout << "Something is wrong with the function!\n";
return 0;
在您确定该功能按预期工作后,您可以将其添加到主程序中。
【讨论】:
以上是关于没有合适的构造函数存在 - 矢量图的主要内容,如果未能解决你的问题,请参考以下文章