Problem with list<string> to a list<string> iterator using list.begin

Hello,
This is my first post here so I thought this would be the appropriate area to post. My problem has to do with learning/using STL list iterator in a for loop.

I keep receiving a error about error C2784. It points to line 40. I have looked up the definition and searched for examples( also in my c++ book ) and it looks like the right way to do this, though it is obviously wrong. Is there something that I am missing?

Current compiling using VS2010 via console.

This is what I have so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <windows.h>
#include <iostream>
#include <string>
#include <list>
#pragma comment(lib, "advapi32.lib")

using namespace std;

int main()
{
    int i=0, j=0;
    char lszValue[100];
    LONG lRet, lEnumRet;
    HKEY hKey;
    DWORD dwLength=100;

    list<string> svchost;
    list<string>::iterator it;
	
    lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "software\\Microsoft\\Windows NT\\CurrentVersion\\Svchost", 0L, KEY_READ , &hKey);
    if(lRet == ERROR_SUCCESS)
    {
         lEnumRet = RegEnumKey(hKey, i, lszValue, dwLength);
         while(lEnumRet == ERROR_SUCCESS)
         {
              i++; j++;
	      // Store results
	      svchost.push_back(lszValue); 
              cout << lszValue << endl;
              lEnumRet = RegEnumKey(hKey, i, lszValue, dwLength);
         }
    }

    cout << "------------------------------------------------------" << endl;
    cout << "There are [" << svchost.size() << "] Svchost services." << endl;
    cout << "------------------------------------------------------" << endl;
    cout << "svchost list contains:" << endl;
    
    // If these 3 lines are commented out and compiled. All is fine but no output obviously.
    for(it = svchost.begin(); it < svchost.end(); it++ ){
	     cout << it << endl;
    }	

    cout << endl;	
    RegCloseKey(hKey);
}


Output w/ lines 40-42 commented out:

iissvcs
LocalService
LocalServiceAndNoImpersonation
LocalServiceNetworkRestricted
LocalServiceNoNetwork
LocalSystemNetworkRestricted
netsvcs
NetworkService
NetworkServiceRemoteDesktopHyperVAgent
NetworkServiceRemoteDesktopPublishing
termsvcs
wcssvc
------------------------------------------------------
There are [12] Svchost services.
------------------------------------------------------
svchost list contains:


and without:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

svchost_check2.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
svchost_check2.cpp(40) : error C2784: 'bool std::operator <(const std::list<_Ty,
_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'co
nst std::list<_Ty,_Ax> &' from 'std::_List_iterator<_Mylist>'
        with
        [
            _Mylist=std::_List_val<std::string,std::allocator<std::string>>
        ]
        c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\list(1588
) : see declaration of 'std::operator <'
...


The purpose of the program is to list services located under the svchost key and display them.
Once again. Looking for a pair of experienced eyes to look at and help point out what I obviously do not know so I can learn from this.

Thanks in return.
You need to de-reference the iterator: cout << *it << endl;

"ERROR_SUCCESS" :p
Hi LowestOne,
I had already dereferenced the interator. I also tried dereferencing the list with no difference. If it helps any, I split the for loop into different lines and now it shows the error is on line 41 with the less than check.
try != instead of <, though I thought < was acceptable for an iterator. It does need the de-reference though.
I actually just tried that before checking this again. Thanks for the quick reply. You hit that right on the dot!

This is what I did to get it to work properly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <windows.h>
#include <iostream>
#include <string>
#include <list>
#pragma comment(lib, "advapi32.lib")

using namespace std;

void main()
{
    int i=0, j=0;
    char lszValue[100];
    LONG lRet, lEnumRet;
    HKEY hKey;
    DWORD dwLength=100;

    list<string> svchost; 
    list<string>::iterator it;
	
	lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "software\\Microsoft\\Windows NT\\CurrentVersion\\Svchost", 0L, KEY_READ , &hKey);
    if(lRet == ERROR_SUCCESS)
    {
         lEnumRet = RegEnumKey(hKey, i, lszValue, dwLength);
         while(lEnumRet == ERROR_SUCCESS)
         {
              i++; j++;
	      // Store results
	      svchost.push_back(lszValue);
              cout << lszValue << endl;
              lEnumRet = RegEnumKey(hKey, i, lszValue, dwLength);
         }
    }

	cout << "------------------------------------------------------" << endl;
	cout << "There are [" << svchost.size() << "] Svchost services." << endl;
	cout << "------------------------------------------------------" << endl;
        cout << "svchost list contains:" << endl;
    
	for(it = svchost.begin();
            it != svchost.end(); // Change to bool value to get rid of error...
                it++ ){
	     cout << *it << endl;
    }	

    cout << endl;	
    RegCloseKey(hKey);
}



End result:

iissvcs
LocalService
LocalServiceAndNoImpersonation
LocalServiceNetworkRestricted
LocalServiceNoNetwork
LocalSystemNetworkRestricted
netsvcs
NetworkService
NetworkServiceRemoteDesktopHyperVAgent
NetworkServiceRemoteDesktopPublishing
termsvcs
wcssvc
------------------------------------------------------
There are [12] Svchost services.
------------------------------------------------------
svchost list contains:
iissvcs
LocalService
LocalServiceAndNoImpersonation
LocalServiceNetworkRestricted
LocalServiceNoNetwork
LocalSystemNetworkRestricted
netsvcs
NetworkService
NetworkServiceRemoteDesktopHyperVAgent
NetworkServiceRemoteDesktopPublishing
termsvcs
wcssvc


Do you happen to know why I have to use != instead of < ??
If you check out Lists:
http://www.cplusplus.com/reference/stl/list/

And then iterators
http://www.cplusplus.com/reference/std/iterator/


You will see that a std lists iterator is a bi-directional iterator, and these do not have < > <= >= abilities (I think).

I will leave learning about lists for you. A list is basically a series of nodes that all link to each other. begin() returns an iterator to the beginning of the list, and end() returns an iterator to the node after the end of the list. Guess what the value of that node is? NULL. You dont want to be doing < > with NULL.
Topic archived. No new replies allowed.