C++, std::list, how to calculate index from iterator - visual-c++

I have a list and I'm using an iterator to locate an item in the list:
std::list<std::string> groupsList = { Constants::Item1, Constants::Item2, ... };
std::list<std::string>::iterator groupListItr = std::find(groupsList.begin(), groupsList.end(), group);
if ( groupListItr == grousList.end() ) {
return;
}
uint16_t groupIdx = (uint16_t)(groupListItr - groupsList.begin());
In the above find call "group" is the string to locate. I'm trying to figure out how to get the position of the found item in the list, if there is an easier way to do this please let me know, the calculation of the index results in an error when compiling:
error C2784: 'unknown-type std::operator -(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(937) : see declaration of 'std::operator -'

The solution:
uint16_t groupIdx = (uint16_t)std::distance(groupsList.begin(), groupListItr);

Related

Template argument with the name of a class in another namespace

In VC++ 2013 (and 2015 RC), I find that this results in compilation errors:
namespace namespace1
{
template<typename T>
class Bar
{};
}
namespace namespace2
{
template <unsigned Bar>
struct Foo
{
static const int value = (Bar < 1) ? 1 : 2;
};
}
Errors:
error C2059: syntax error : ')'
: see reference to class template instantiation 'namespace2::Foo<Bar>' being compiled
error C2143: syntax error : missing ')' before ';'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ';' before ')'
error C2059: syntax error : ')'
error C2238: unexpected token(s) preceding ';'
fatal error C1201: unable to continue after syntax error in class template definition
If I swap the order of the namespaces, I don't get an error.
Why is the compiler treating Bar as a type, when it's not qualified with the namespace?
Additionally, if I change the value initialisation to:
static const int value = (Bar > 1) ? 1 : 2;
I don't get an error either.
Encountered this while compiling Google Protocol Buffers, where this struct definition:
// Compile-time equivalent of VarintSize32().
template <unsigned Value>
struct StaticVarintSize32 {
static const int value =
(Value < (1 << 7))
? 1
: (Value < (1 << 14))
? 2
: (Value < (1 << 21))
? 3
: (Value < (1 << 28))
? 4
: 5;
};
won't compile due to a template class called Value existing in a namespace in our own codebase. I have worked around it for now by ensuring that the relevant Procotol Buffers header is included first, but it seems that this is a compiler bug?
Is there any other way to fix it, when I can't really change either Protocol Buffers code or the Value class?
Confirmed as a bug by Microsoft, will be fixed in 2015 RTM.

Cannot convert from 'cli::array<Type> ^' to 'cli::array<Type> ^'

C++ CLR, in Visual Studio 2010 (C++).
I have this struct:
value struct Triangle{
static array<int>^ v = gcnew array<int>(3);
static array<int>^ t = gcnew array<int>(3);
static array<int>^ n = gcnew array<int>(3);
};
and I declare the following in "private" section of my class:
static array<Triangle^>^ triangles = gcnew array<Triangle>(MAX_POLYGONS);
I get this error and I have no idea what it means (because it seems to contradict itself):
error C2440: 'initializing' : cannot convert from 'cli::array<Type> ^' to 'cli::array<Type> ^'
with
[
Type=Triangle
]
Evidently it doesn't like the struct. Should I be using ref instead of value? That produces a lot of pointers. Any push into the correct direction would be appreciated.
You just have an extra hat (^) in your statement. It should read:
static array<Triangle>^ triangles = gcnew array<Triangle>(MAX_POLYGONS);

how visual studio tell c++ and c?

As the title says,does the visual studio distinguish these two files by their suffix?.c or .cpp?
I also have another question.At first,I stated the program like this:
int main(int argc, char **argv)
{
LARGE_INTEGER TimeStart;
LARGE_INTEGER TimeEnd;
QueryPerformanceCounter(&TimeStart);
static double Freq;
static int getfreq;
double mu,om;
double *v;
int it,i,j;
....
}
but it brings out many problems:
1>sor2d.c(23): error C2143: syntax error : missing ';' before 'type'
1>sor2d.c(24): error C2143: syntax error : missing ';' before 'type'
1>sor2d.c(25): error C2143: syntax error : missing ';' before 'type'
1>sor2d.c(26): error C2143: syntax error : missing ';' before 'type'
23 ling points to "static double Freq;"
but if I put "QueryPerformanceCounter(&TimeStart);" after the data allocation,the compiler can succeed.Could someone tell me why this happened,was is just because of my carelessness of omitting something or ignorance...?
In C, all variables must be declared before calling any methods.
Visual Studio will, by default, compile .C files as C. You can override this.
In C89, you must declare all of your variables at the top of the code block. You may also initialize them to compile-time constants (literals, macros that expand to literals, the values of variables that have already been initialized, and any operations on the above that can be performed at compile time). You cannot intersperse other types of statements (like function calls) within these declarations.
This limitation was removed in C99 (which is not supported by Visual C++) and C++.

Undeclared Identifier: Probably a very simple fix

Language: Visual C++, MFC
I'm attempting to make an array of pointers to CString variables that I have declared in my header file. In general, this is what I'm doing:
CString *variableArray[5] = {
&var1
, &var2
, &var3
, &var4
, &var5
};
For whatever reason, though, I keep getting the following error:
Error 1 error C2065: 'var1' : undeclared identifier 18
Error 2 error C2065: 'var2' : undeclared identifier 19
Error 3 error C2065: 'var3' : undeclared identifier 20
Error 4 error C2065: 'var4' : undeclared identifier 21
Error 5 error C2065: 'var5' : undeclared identifier 22
I'm not quite sure I'm getting this error. To me knowledge, this is the correct way to make an array of pointers. Any help would be awesome!
EDIT: Here are the declarations in the header file:
public:
CString var1;
CString var2;
CString var3;
CString var4;
CString var5;
Where do you create "variableArray"? If it's in a static method or outside the scope of the class, it would make sense why you're getting that error.

cannot convert parameter 1 from 'ATL::CString' to 'const wchar_t *'

For this line of code:
int currentSnapshotHeight = _wtoi(ExecuteExternalProgram(L"current.png"));
I got this error:
Error 1 error C2664: '_wtoi' : cannot convert parameter 1 from 'ATL::CString' to 'const wchar_t *'
How to fix?
Maybe this will work?
int currentSnapshotHeight = _wtoi(ExecuteExternalProgram(_T("current.png")));
Also check if Unicode setting of project are set as expected.
Try this:
int currentSnapshotHeight = _wtoi((wchar_t*)ExecuteExternalProgram(L"current.png").GetBuffer());

Resources