Hello fellows, I am trying to slice any given row from 2D matrix. I receive the errors
Error C3867 'std::vector<double,std::allocator<_Ty>>::size': non-standard syntax; use '&' to create a pointer to ..
Error C2446 '<': not converting 'unsigned int (__thiscall std::vector<double,std::allocator<_Ty>>::* )(void) noexcept const' to 'int...
What's wrong in my code? Thank you in advance.
Original matrix is = Demand=[[ 3 6 8 9
[ 4 9 12 19]
[20 100 56 90]]
1 2 3 4 5 6 7 8 9 10
vector<double> getRow( matrix &original, int value)
{
vector<double> result2;
for (int t = 0; t < original[0].size;t++) result2.push_back(original[value][t]);
return result2;
int main()
vector<double>D = getRow(Demand, x);
}
where da closing bracket before main?
that would cause all sorts of 'exciting' verbiage to spew forth from the compiler.
even more weird, main lacks an opening brace, so its all mixed up in there.
valarray is a lot like a vector and has a friendly slice built in.
if original is a masked vector, size() may be a function, not a member. That could also cause some excitement.
if original is a vector of vectors, returning a reference to a row may work better than returning a copy of a row.
1. Error messages include line numbers, so saying which line(s) the errors relate to is a bonus.
2. http://www.sscce.org/
Stripping out a handful of lines just to make for a short post, whilst removing a lot of context doesn't help.
Just removing random braces makes even what you posted tough to just copy/paste.
The more work you make us do to help you, the less chance it's going to happen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
#include<string>
#include<vector>
usingnamespace std;
vector<double> getRow(vector<vector<double>> &original, int value)
{
vector<double> result2;
for (int t = 0; t < original[0].size;t++)
result2.push_back(original[value][t]);
return result2;
}
int main()
{
vector<vector<double>> mat{};
vector<double>D = getRow(mat,3);
}
1 2 3 4 5 6 7
main.cpp: In function 'std::vector<double> getRow(std::vector<std::vector<double> >&, int)':
main.cpp:9:41: error: invalid use of member function 'constexpr std::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::size() const [with _Tp = double; _Alloc = std::allocator<double>; size_type = long unsigned int]' (did you forget the '()' ?)
9 | for (int t = 0; t < original[0].size;t++)
| ()
So there are two things wrong here.
1. size is a member function, so you need ()
2. The size of a vector is usually an unsigned type, not an int.
Assuming matrix is some thin gloss over the top of a 2D vector, then something like this.
for (vector<vector<double>>::size_type t = 0; t < original[0].size();t++)
What is the OP's definition of "matrix," and what is the definition of "slicing?"
IMO slicing a 2D vector isn't as simple as creating a 1D vector from a selected 2D row. It could be a vector from a select column, or a diagonal slice. Or selecting a subset of the elements, say based on whether the elements are odd or even.
M'ok, this is really "going off into the weeds," especially since the OP is reluctant/unable to provide more in-depth information to understand what they are trying to do.