Visual Studio 2010

I just started using VC10 and I am getting the following error, could anybody try to pop in with a fix?

1> consoleC++Test.cpp
1>c:\projects\programming\windows\c++\code\gameclub\consoleaplic\consolec++test\consolec++test\consolec++test.cpp(9): error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::size': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::size' to create a pointer to member
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>c:\projects\programming\windows\c++\code\gameclub\consoleaplic\consolec++test\consolec++test\consolec++test.cpp(9): error C2446: '<=' : no conversion from 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Ax>::* )(void) const' to 'int'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> There is no context in which this conversion is possible
1>c:\projects\programming\windows\c++\code\gameclub\consoleaplic\consolec++test\consolec++test\consolec++test.cpp(9): error C2297: '<=' : illegal, right operand has type 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Ax>::* )(void) const'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]


This is the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include "windows.h"

void timerFunction ( std::string a)
{
	int i;
	for (i = 0 ; i <= a.size ; i++)
	{
		std::cout << a[i];
		Sleep(1000);
	}
}

void main()
{
  int iTest;
  std::string sTest = "Hello world!";
  timerFunction(sTest);
  std::cin >> iTest;
}
@bleepblop

You needed to add #include <string> for using a string variable,
and change a.size; to a.size();
I added another variable, I named len, for the length of the string, and used that in the for loop. It removed a few warnings.
1
2
3
4
5
6
7
8
9
10
void timerFunction ( std::string a)
{
	int i, len;
	len = a.size();
	for (i = 0 ; i < len ; i++)
	{
		std::cout << a[i];
		Sleep(1000);
	}
}
Last edited on
Thank you, it runs now but after it ends I get the following error :

http://imageshack.us/f/703/83417552.jpg/

Also, whatever I click next, I recieve another pop up saying that it caused a breakpoint and then I get the following source file :

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
// throw -- terminate on thrown exception REPLACEABLE
#define _HAS_EXCEPTIONS 0
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <crtdbg.h>

_STD_BEGIN

#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
	{	// report error and die
        if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
        {
            ::_CrtDbgBreak();
        }
	}
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
	{	// report error and die
        _Debug_message((wchar_t *) message, (wchar_t *) file, line);
	}

#endif

_STD_END

/*
 * Copyright (c) 1992-2007 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 V5.03:0009 */
@bleepblop

Looking at the jpg, I see you have the for loop as for (i = 0 ; i <= len ; i++) which will cause the loop to read outside of the string. Remove the equal sign part in the loop, and it should be fine.

Reason:

Let's say your sText equals "Welcome to programming". Len would then equal 22. But, with the <=, the for loop would count from 0 to 22, making 23 letters. By using just the < sign, it now counts from 0 to 21, making 22, staying inside the sText length.
Last edited on
Topic archived. No new replies allowed.