Unable to add watch on a class variable

I have a string array data[10843] declared as a private variable in my class header file and being used in my class cpp file. I am trying to debug and observe whether a function that is using this variable is working properly but every time that I try to add a watch to this variable there's a red x next to the name of the variable and its value description is 'identifier "data" is undefined'. Is there a way to view this variable or any variable inside a class? If not why isn't it possible to do this? Is there any other way around this or another way to view the variable other than std::cout ? I am using Microsoft Visual Studio 2015.
Thanks in advance.

Class CPP 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
#include "KPI.h"
#include <iostream>
#include <string>
#include <fstream>
#include <Windows.h>
#include <ctype.h>

void KPI::readfile(std::ifstream &object, std::ofstream &oobject)
{
	a = 0;
	while (object.good())
	{
		std::getline(object, data[a]);
		for (std::string::size_type i = 0; i < data[i].size(); ++i)
		{
			if (!isascii(data))
			{
				data[i].erase(i,0);
			}
		}
		oobject << data[a] << std::endl;
		std::cout << data[a] << std::endl;
		a++;
	}
}


Class Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include <fstream>

class KPI
{
public:
	KPI();
	~KPI();

	void readcheck(std::ifstream &object);
	void readfile(std::ifstream &object, std::ofstream &oobject);

private:
	int input;
	//int i;
	int a;
	std::string data[10843];
};


Source CPP File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <fstream>
#include <Windows.h>
#include "KPI.h"


KPI kpi;

int main()
{
	std::ifstream ifile("document.csv");
	std::ofstream ofile("data.txt");
	kpi.readcheck(ifile);
	kpi.readfile(ifile, ofile);
	ifile.close();
	ofile.close();

	return 0;
}
Last edited on
Show code, including any #includes or #ifndefs etc.

Is data defined within the class, or globally?
Last edited on
I added the code. The variable is defined inside the class.
Last edited on
Thanks, but still with the information given, I still can't reproduce your error. I was hoping it would be more obvious.

But the good news is, I think it might just be an IntelliSense error, and not an actual compiler error. Have you actually tried compiling it? If not, try it, and write the error message that the compiler gives.

Long story short: I think the compiler will spit out a different, real error for you.
Something like:
41:21: error: invalid conversion from 'std::string* {aka std::basic_string<char>*}' to 'int' [-fpermissive]
,
originating from
if (!isascii(data))

isascii is not a standard C++ function. And besides, it takes in an integer (it expects a character), not an array of std::strings.

I think you meant to do something like this:

1
2
3
4
		for (std::string::size_type i = 0; i < data[a].size(); ++i)
		{
			if (!isascii(data[a][i]))
			{


If isascii is not defined for you, perhaps try a different function, like isprint.
http://www.cplusplus.com/reference/cctype/isprint/
It is used in the same way that isascii is used, although has slightly different behavior.
Last edited on
I was using the isascii function to read through each character of the string and then replace any character that isn't in the ascii table. I will try the isprint function. Also the program does compile and run, however the only issue I was having was being unable to add a watch to the string variable and observe the data in the variable. I am able to add the watch, I just can't view the data in the variable.
Oh, I see, you're talking about looking at variables during debugging. The fact that your program even compiles is concerning though. Have you tried applying my change doing (not just isascii vs isprint, but the other part of my change). I would increase the warning level of your compiler, it should be at least a 3 if you're using visual studio.

I'm actually not sure what compiler you're on, so I'm assuming Visual Studio.

Okay, that I'm a lot less knowledgeable about, not that I don't use a debugger, but that I've never had that particular issue.

Are you compiling with debugging symbols? (I think these are automatically created when compiling in Debug as opposed to Release in Visual Studio, but for compilers like GCC, it's the -g option).
Last edited on
The fact that your program even compiles is concerning though


Why is it concerning? Is there an error that I'm not seeing?

Have you tried applying my change doing (not just isascii vs isprint, but the other part of my change).

Yes I have tried both and the isprint gives me an error. However the isascii works fine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void KPI::readfile(std::ifstream &object, std::ofstream &oobject)
{
	a = 0;
	while (object.good())
	{
		std::getline(object, data[a]);
		for (std::string::size_type i = 0; i < data[a].size(); ++i)
		{
			if (!isascii(data[a][i]))
			{
				data[a].erase(i,0);
			}
		}
		oobject << data[a] << std::endl;
		std::cout << data[a] << std::endl;
		a++;
	}
}


The isprint error states:

Debug Assertion Failed!
Program: ...Visual Studio
2015\Projects\MiniProject\Debug\MiniProject.exe
File: minkernel\ctrs\ucrt\src\appcrt\convert\isctype.cpp
Line: 36

Expression: c >= -1 && c <= 255

For more information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts

(Press Retry to debug the application)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void KPI::readfile(std::ifstream &object, std::ofstream &oobject)
{
	a = 0;
	while (object.good())
	{
		std::getline(object, data[a]);
		for (std::string::size_type i = 0; i < data[a].size(); ++i)
		{
			if (!isprint(data[a][i]))
			{
				data[a].erase(i,0);
			}
		}
		oobject << data[a] << std::endl;
		std::cout << data[a] << std::endl;
		a++;
	}
}


I'm actually not sure what compiler you're on, so I'm assuming Visual Studio.

Yes I am currently using Visual Studio 2015
Why is it concerning?
Because isascii and isprint expect an int as input, and previously you were giving it an array of std::strings (data), which is completely incompatible.

I'm not even sure how an assertion like that is possible to fail if data[a][i] is a character.

At this point, if someone still can't figure it out, I would work on making a minimal, compile-able example that reproduces your issue. Try to make the program as small as possible, preferably one file, that we can reproduce the issue with.
http://sscce.org/

Because right now, I don't have enough information to actually run your program and see what's happening myself.
Last edited on
Is there a way to view this variable or any variable inside a class?
Actually when you step inside a member function of class KPI you can watch the data member. If you want to watch it outside the member functions you need to watch the entire global variable. I.e. add kpi.data to your watch list.
@Ganado I may have misunderstood how is ascii works. I assumed that it works by looking at each character and converting it into an int and checking if that value is in the ascii table. I will try to reproduce that function error and see if I can solve it. Thanks for the assist.

@coder777 I'm not well versed in using the debugger so I'm unsure how to do that in Visual Studio. I have not tried it yet but I will try it in a couple of minutes and post if I succeeded.
Also, just be clear: Note that char is a type of int (just smaller range). So a function like isascii or isprint will happily accept a character and convert it to an int, it just doesn't know how to deal with the whole string or array of strings.
Topic archived. No new replies allowed.