Attempting to print SerialPort inputs to the console - c++-cli

First my code:
#include <iostream>
#include <thread>
#using <System.dll>
using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
int main()
{
SerialPort^ mySerialPort = gcnew SerialPort("COM5");
mySerialPort->BaudRate = 9600;
mySerialPort->Parity = Parity::None;
mySerialPort->StopBits = StopBits::One;
mySerialPort->DataBits = 8;
mySerialPort->Handshake = Handshake::None;
mySerialPort->RtsEnable = true;
while (1)
{
Console::WriteLine(Console::ReadLine());
}
}
The idea was to read from the SerialPort and write to the console. Source
Originally I was going to use:
std::cout << Console::ReadLine() << '\n';
However, that had an error (ReadLine outputs String^ not String, I don't know the difference) and I was hoping for something to compile.
With the above code I received the error:
two-phase name lookup is not supported for C++/CLI ... use /Zc:twoPhase-
The error recommends I use /Zc:twoPhase- which is a compiler option. So I enabled it and got the error:
Element has an invalid value of "Yes(/permissive-) /Zc:twoPhase-"
I'm not quite sure how to proceed from here.
Apologies I'm a beginner and I dove way over my head. Any help would be appreciated!
Note: I included thread, I know this code doesn't use it, but I plan on using it later.

Judging by the "Element has an invalid value of "Yes(/permissive-) /Zc:twoPhase-"" you've put this compiler option where it does not belong. Make sure you know where it should go. E.g. Why do I have warning "C4199 two-phase name lookup is not supported for C++/CLI, C++/CX, or openmp"?

There is no solution. I got an "cannot open file 'MSCOREE.lib'" error. It appears that file no longer exists in Windows and I didn't know how to get ahold of it. So I used the Visual Studio Windows Form App.
Edit: eXCore's comment about the .NET framework solved it.

Related

Clion IDE, Whenever i create new file gives error [duplicate]

In the journey to learning C++ im learning through the C++ Manual thats on the actual website. Im using DevC++ and have hit a problem, not knowing whether its the compilers error or not.
I was going through this code bit by bit typing it in myself, as I feel its more productive, and adding my own stuff that ive learnt to the examples, then I get to initialising variables. This is the code that is in the C++ manual
#include <iostream>
using namespace std;
int main ()
{
int a=5; // initial value = 5
int b(2); // initial value = 2
int result; // initial value undetermined
a = a + 3;
result = a - b;
cout << result;
return 0;
}
This is popping up a compiler error saying " Multiple definitions of "Main""
Now This is on the actual C++ page so im guessing its a compiler error.
Could someone please point me in the right direction as to why this is happening and what is the cause for this error.
Multiple definitions of "main" suggests that you have another definition of main. Perhaps in another .c or .cpp file in your project. You can only have one function with the same name and signature (parameter types). Also, main is very special so you can only have one main function that can be used as the entry point (has either no parameters, one int, or an int and a char**) in your project.
P.S. Technically this is a linker error. It's a subtle difference, but basically it's complaining that the linker can't determine which function should be the entry point, because there's more than one definition with the same name.
Found I had two file references in my tasks.json file that were causing this error and which took me a long time to figure out. Hope this helps someone else..... See "HERE*****" below:
"-I/usr/include/glib-2.0",
"-I/usr/lib/x86_64-linux-gnu/glib-2.0/include",
//"${file}", //HERE**********************
"-lgtk-3",
"-lgdk-3",
"-lpangocairo-1.0",
"-lpango-1.0",
"-lharfbuzz",
"-latk-1.0",
"-lcairo-gobject",
"-lcairo",
"-lgdk_pixbuf-2.0",
"-lgio-2.0",
"-lgobject-2.0",
"-lglib-2.0",
"-o",
"${fileDirname}/${fileBasenameNoExtension}" //HERE*************
],
When I practiced CMake, I encountered the same problem. Finally, I found that the source code path set in the cmakelist project was incorrect. As a result, the compiled files included many duplicate files generated during CMake execution. As a result, compilation errors occurred

can someone aid me solving error winmain#16 with simple hello world project

#include <iostream>
int Main();
{
std::cout <<"hello world\n";
return 0;
}
This is simple yet i get "undefined reference to 'winmain#1'" i have tried replacing int main with int winmain. But the same problem still occurs. can use some professional help please. Good vibes please <3
I see two issues here:
Not adhering to proper main method name. It should be main() and not Main()
Misplaced semicolon after int Main(). That terminates the definition and the code that follow is just having a different scope altogether.
If you make these changes, your code should compile successfully.

Disable Syntax Error "Symbol <id> could not be resolved" for some symbols in Eclipse Plugin using CDT

In my eclipse plugin I want to support my tool's language which extends C++ with some keywords and concepts. My language class, editor class and source parser class are all inheriting CDT classes for C++. I can parse the keywords and add nodes for them to the AST. But some of my keywords/commands the editor will always mark as "Symbol could not be resolved".
Example:
There is a command "#result" which returns the result of a last computation as an enum value that is defined in some header file in the tool's core.
typedef enum {
OK = 0;
WARNING = 1;
ERROR = 2;
} errCode_t;
So the command #result returns 0, 1 or 2. But inside the editor the command is marked as Symbol '#result' could not be resolved. No I want to tell the Indexer to not try to resolve this very token.
In the Preprocessor class I could change the token type from IToken.tIDENTIFIER to, say, 50000. What I try to achieve by that is something like
if (token.getType() == 50000) {
// don't try to resolve symbol
return null;
} else {
return super.resolveSymbol();
}
Is there a way to do that? I think my first problem is that I don't understand who or what is responsible for the Syntax Error Marking (maybe the Indexer?).
Errors of the form Symbol ... could not be resolved are produced by CDT's Code Analysis component, specifically ProblemBindingChecker, which traverses the AST and reports the error for any IASTName which resolves (via IASTName.resolveBinding()) to a ProblemBinding.
It is only IASTName nodes which resolve to bindings, so if you are getting this error for your #result token, that suggests the parser is building an IASTName node for it. I'm not sure how that's happening if you've changed the token type, I suppose it depends on how you handle the new token type in your extended parser.

I am trying to run older VC++ code in 2010VC++ I am getting error C387 error

this->InitButton->Location = System::Drawing::Point(24, 8);
this->InitButton->Name = S"InitButton";
this->InitButton->Size = System::Drawing::Size(184, 24);
this->InitButton->TabIndex = 0;
this->InitButton->Text = S"Initialize NMC Network";
// this give an error
this->InitButton->Click += new System::EventHandler(this, InitButton_Click);
this->InitButton->Click += gcnew System::EventHandler(this, InitButton_Click);
That's C3867, not C387. You have to specify the class name with the method name in C++/CLI. It's syntax for assigning delegates is quite unlike the one in the C# language, there's no syntax sugar at all. This isn't otherwise associated with VS2010, it fails to compile in earlier editions too. Fix:
this->InitButton->Click += gcnew System::EventHandler(this, &Form1::InitButton_Click);
Replace Form1 with the name of your Form derived class. You don't actually need & but it is boilerplate in the designer generated code. Letting the designer generate this code is the best way to keep out of trouble.
In addition to Hans's answer concerning the correct way to get a pointer-to-member for delegate construction, C++/CLI does not use the S prefix on managed strings.
Thanks,
You are right about the error code (typo). I was able to compile is with warning now. I made some changes.
Changed common language run time support to old syntax and was able to compile and run it.
Common Language Runtime Support, Old Syntax (/clr:oldSyntax)

Placing function in .dll causes debug assertion failed error

Hallo fellow members,
I run into a very strange problem today and I am not exactly sure as to what is causing it. Here is a function which I use to get the current working directory :
#ifdef _WIN32
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#error "There is currently no support for non windows based systems!"
#endif
const std::string getCurrentPath()
{
char CurrentPath{_MAX_PATH];
GetCurrentDir(CurrentPath, _MAX_PATH);
CurrentPath[_MAX_PATH - 1] = '/0';
return std::string(CurrentPath);
}
This function works well as a stand alone function. However if I declare it as a static function inside a class :
static __declspec(dllexport) const std::string getCurrentPath(void);
and a .dll I get "debug assertion failed error" when I try to do
std::cout<<CUtilities::getCurrentPath()<<std::endl;
If I instead write :
std::string dir = CUtilities::getCurrentPath();
std::cout<<"Dir is : "<<dir<<std::endl;
it works fine. I am totally confused as to what I am doing wrong. Any ideas?
I finally found out what the problem was. The project was compiled with /MT options , therefore the .dll had a different heap than the original file. So when the string size was bigger than it's initial size (15) then heap was allocated from the .dll's side. However the string had it's destructor called from the main program side and then the destructor was trying to deallocate memory from the .dll's heap thus resulting in "heap corruption error"
The solution was to simply compile with /MD options.