Phantom syntax errors in Visual C++ 2010 Express - visual-studio-2010

I just started learning C++, and I'm getting weird 'phantom' syntax errors in Visual C++ 2010 Express.
There are red lines under seemingly random pieces of code, and when I hover my cursor over them it shows errors that seem to make no sense at all. However, when I hit F5 the program compiles and runs successfully.
It's hard to learn C++ like this because I can't quickly distinguish between real syntax errors and 'fake' ones.
The syntax errors:
http://i.stack.imgur.com/O0UbD.png
The program:
#include <iostream>
#include "conio.h"
#include "windows.h"
class test2
{
public:
int i;
};
class testc
{
public:
test2 hi;
};
int main()
{
testc hello;
hello.hi.i = 23;
std::cout << hello.hi.i << "\n";
system("pause");
}

I reinstalled entire Visual Studio, not just Visual C++. Now it works correctly.

Related

Visual Studio 2022 not allowing c++20 even though i have got /std:c++20

I have got the following code:
#include<iostream>
#include<ranges>
#include<string>
auto f(auto& x, auto& y)
{
std::cout << x << y << "\n";
}
struct s
{
int x;
std::string y;
};
int main()
{
std::cout << "Hello World" << "\n";
}
and I have the /std:c++20 in my configuration:
ISO C++20 Standard (/std:c++20)
I have tried using that and I have the following error:
Severity Code Description Project File Line Suppression State
Error (active) E1598 'auto' is not allowed here Project1 C:\Users\robert\source\repos\Project1\Project1\Source.cpp 5
1>Source.cpp
1>The contents of <ranges> are available only with C++20 or later.
1>C:\Users\rober\source\repos\Project1\Project1\Source.cpp(5,15): error C3533: a parameter cannot have a type that contains 'auto'
1>C:\Users\rober\source\repos\Project1\Project1\Source.cpp(5,24): error C3533: a parameter cannot have a type that contains 'auto'
I am expecting my code to run fine because all of the c++20 headers are fine like std::ranges etc. It is by default on Visual Studio 2022 that the language is C++14 but I do not know why it is not running. So, it just resulted in the error shown above.
I have seen the other question about auto is not allowed here but that was in c++17 and the answer to that was it will come with c++20, but i have /std:c++20 in my projects -> propeties bit of visual studio 2022

C++ Segmentation Fault while using online Compiler but the Same works in VS Code

I am new to C++ Programming. I'm getting Segmentation Fault error while compiling my code in online compilers but when I try compile it using Visual Studio Code and g++ in offline(means in my local machine) works fine.
The Code I have tried is
`
#include <iostream>
int main() {
int *ptr;
*ptr = 10;
cout<<*ptr; //Prints 10
cout<<ptr; //Prints Some garbage address
}
But the above program not working in online compilers(used onlinegdb).
My machine configuration
g++ 11
Visual Studio Code 2016
This line *ptr = 10; is fundamentally wrong, as you cannot assign value by dereferencing a pointer.
The correct method of doing so, would be:
#include <iostream>
using namespace std;
int a=10;
int *ptr;
ptr=&a;
cout<<ptr<<endl;
cout<<*ptr<<endl;

Linking gRPC on Windows for VisualC++

I am trying to use gRPC in a Visual C++ project.
So far I have:
1) Build gRPC with vcpkg: vcpkg install grpc:x64-windows
2) Integrated the vcpgk libraries with visual studio: vcpkg integrate install
So far, so good - intellisense autocompletes the namespace etc.
My client cpp file looks like this:
#include "pch.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp\grpcpp.h>
#include "GRPCServerInterface.grpc.pb.h"
#include "FileFormat.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using namespace GRPCServerInterface;
int main()
{
std::cout << "Hello World!\n";
// prepare send message & payload
IsFormatSupportedInput msg;
msg.set_fileextension(".asp");
// prepare reply
IsFormatSupportedOutput rpl;
// connect
FileHandler::Stub ClientStub = FileHandler::Stub(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
ClientContext context;
// execute rpc
Status status = ClientStub.IsFormatSupported(&context, msg, &rpl);
// handle result
if (status.ok())
{
std::cout << "Format supported says:" << std::endl << "\t formats read: " << rpl.readsupportedformats() << std::endl << "\t formats write: " << rpl.writesupportedformats() << std::endl;
}
else
{
std::cout << status.error_code() << ": " << status.error_message() << std::endl;
}
}
All messages & proto files exits and work in general, since I already use them in python and c# projects.
When building, Visual Studio generates a boatload of 125 errors, all in files I never touched.
In GRPCServerInterface.pb.h, there is identifier GOOGLE_DCHECK is undefined
All other errors are member abc may not be initialized in various header files in the grpc includes, for example
member "google::protobuf::Any::kIndexInFileMessages" may not be initialized in file any.pb.h. Many more in type.pb.h and descriptor.pbp.h.
Last but not least, I get prompted add #iclude "pch.h" to the auto-generated protobuf classes grpcserverinterface.grpc.pb.cc and grpcserverinterface.pb.cc - adding it changes a bit, but basically all errors are still undefined symbol and member may not be initialized. And I really do not want to modify auto-generated code every time.
What am I missing? Or is it just a fruitless endeavor to try using grpc with Visual Studio and should I just move to a build framework like bazel?
Solved it!
Two steps for solving:
1) I disabled precompiled headers for the whole project - this made the #include "pch.h go away. You could probalby get away with disabling it just for the protobuf files, as it can be done on a per-file basis.
2) One of the last errors listed was unresolved external symbol __imp_WSASocketA, which finally led me to this question Unresolved external symbol LNK2019. I just included #pragma comment(lib, "Ws2_32.lib") in one source file, and now everything works just perfect.

Unable to debug c++ program with class implementation

I am trying to setup debugging environment in visual studio and I hope I was successful in doing it,because when I am trying to debug a normal math c++ program then my break point is hitting(or first line of code as default breakpoint) but when I am trying to debug the main program with class header file name included in the main program(i.e with class object or class implementation) then my breakpoint are not hitting. Any help where I am going wrong??
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main()
{
cout<<"hello";
Sales_item item1, item2;
cin >> item1 >> item2; // read a pair of transactions
cout << item1 + item2 << endl; // print their sum
return 0;
}
This is showing in cppdgb console:
C:\Users\admin\Documents\cpp>c:\Users\admin.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe
--stdin=Microsoft-MIEngine-In-w2a4a5fw.4hb --stdout=Microsoft-MIEngine-Out-q2oc4jsv.b14 --stderr=Microsoft-MIEngine-Error-rlvf2qhl.2bw --pid=Microsoft-MIEngine-Pid-3fayxkbu.ji4 --dbgExe=C:\MinGW\bin\gdb.exe --interpreter=mi

Debugging file scope in Visual Studio

In Visual Studio 2012, I have this uber-simple C++ program:
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << std::cout;
int i = 1;
return 0;
}
In case you wonder, the "std::cout << std::cout" part is only to show that std:cout can be accessed within the scope of the function - yes, it's meant to print the pointer to the console.
So I set a breakpoint at "int i = 1". When the breakpoint triggers, I want to inspect std::cout from the Command Window (or Immediate Window), so I type:
>Debug.Print std::cout
But it returns the following error:
identifier "std" is undefined
I don't understand why this happens, std should be in the scope of the function which is being executed. The same goes for any other stuff coming from the #include directive, I just can't inspect it using Debug.Print. What do I use in Visual Studio 2012 to inspect EVERYTHING accessible in the execution scope? It seems I can only access local variables with Debug.Print.

Resources