Program output is correct, but VS reports an error during step-by-step debugging

zzzhhh 40 Reputation points
2024-05-29T01:58:25.83+00:00

The C++ code snippet is:

class A {
public:
    A(const std::string& ss, const std::string& tt) :
		s(ss), t(tt) {} 
	void func1() const { std::cout << s; }
	void func2() const { std::cout << t; }
    using Action = void (A::*)() const;
	void test(Action act = &A::func1) const {
		(this->*act)();
	}
private:
	const std::string s;
	const std::string t;
};

If I step-by-step debug the following code (it's in main(), but the webpage incorrectly marks it as SQL):

	A a("abc","def");
	a.test();

There is no problem. The output is abc.

However, if I step-by-step debug the following code:

    A a("abc","def");
    a.test(&A::func2);

The program can still outputs def as expected, but VS reports the following error when the execution enters the test member function:

User's image

I don't understand the error because my code does not try to open any file. Can you please help me figure out why this error is caused and how to remove it in step-by-step debugging? I am using Visual Studio 2019 on Windows 10. Thanks.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,205 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,764 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
1,005 questions
{count} votes

Accepted answer
  1. Tianyu Sun-MSFT 31,326 Reputation points Microsoft Vendor
    2024-05-29T06:26:41.2266667+00:00

    Hello @zzzhhh,

    Welcome to Microsoft Q&A forum.

    Please try to close VS, remove the hidden .vs folder in your solution folder, rebuild and debug your project again.

    Please also try to repair VS(VS Installer => More => Repair, this may take some time).

    Besides, if you create a new C++ project and use the same code, will this error appear on this newly created project?

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.