I tried to build really basic associative array but it turned out I don't have enough knowledge, so I tried something less, and less, and less, because I kept failing at it. Finally I just tried to set variables and print them in the command-line. But I can't even do that, and this is like ridiculous begin of all the programming and that even failed. I can't precisely understand what error means and Googling refers to other programming languages or errors that don't match mine. The code I have:

#include <string>
#include "stdafx.h"
#include <limits>
#include <iostream>

int main() {
    const System::Int64 TheNumbah = 1844674407370955161;
    const std::string MarcoPolo = "Yupikayay!";
    System::Console::WriteLine(MarcoPolo);
    System::Console::WriteLine(TheNumbah);

    std::string s;
    std::getline(std::cin, s);
}

Where did I go wrong? The first System:: line, especially System::Console::WriteLine has red underline, most likely related (and the only) error is:

IntelliSense: no instance of overloaded function "System::Console::WriteLine" matches the argument list argument types are: (const std::string)

Error doesn't say anything to me, and the syntax for this basic task is so simple I'm surprized I failed at it.

Could anybody help me solve this issue?

The operating system I tried it on is Windows 7 x64, codes from C++11 work in my IDE (so I expect that onward versions will work as well) which is Microsoft Visual Studio Community 2013, Update 4.

Recommended Answers

All 16 Replies

System::Console::WriteLine is C# (and probably various other .NET langauges). You're trying to code in C++, right? You can't use constructs from entirely different programming languages.

In C++, simple console output is done with cout.

http://www.cplusplus.com/doc/tutorial/basic_io/

It's not even C# (should have been System.Console.WriteLine), it's a combination of static function call from C++ with objects from C#. How Moschops said ... go with cout.

Actually, this is what I believe is now called 'C++ for .Net Applications', what used to be called 'Managed C++' (or as I always preferred, 'Mangled C++'). It is a dialect created by Microsoft and specific to their systems, which runs on the .Net CLR. It is really a new language, distinct from standard C++, but MS never wanted to admit this as it would put off those who didn't want to work with .Net (and to silently lock client-programmers into their non-standard dialect without them realizing it). It exists primarily because they were never able to move all the system operations to .Net as they originally planned, and so continued to need ways to mix .Net code with native code (as I understand things, their real goal in creating .Net was to give the x86 platform the heave-ho - something nearly everyone, including Intel, would like to do - by making Windows itself run in the .Net pseudo-machine a la the old UCSD Pascal OS, but it never came close to working since it would mean losing the entire backwards compatibility structure that Windows' success is built upon, and the goal was eventually abandoned).

Well, I also thought at first at "Managed C++" but I never actually used such a compiler (or interpreter probably) so I don't know the syntax and resumed that he must have made a mistake. Anyway, the problem here is that the System class is not included. Try something like

using namespace System;

Put this before main ... it should work.

no instance of overloaded function "System::Console::WriteLine" matches the argument list argument types are: (const std::string)

I've never used C++/CLI, but it looks like std::string is a distinct type from System::String and presumably Console::WriteLine only works with the latter as it is a .net method and std::string is a C++ class.

Presumably C++/CLI offers some way to convert between the two string types, but I imagine in this case it'd be easiest to just either only use C++ strings and C++ IO functions or .net strings and .net IO functions.

Anyway, the problem here is that the System class is not included.

No, it's not. using namespace System; just means that you're then allowed to write Console::WriteLine instead of System::Console::WriteLine. It does not change for which types the WriteLine method has overloads.

Moschops

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {

    const System::Int64 TheNumbah = 1844674407370955161;
    const std::string MarcoPolo = "Yupikayay!";
    cout << MarcoPolo << endl;
    cout << TheNumbah << endl;

    return 0;
}

Results in: Error 1 error C1083: Cannot open include file: 'stdafx.h': No such file or directory. The funny answer I got is "just delete it"... but it's not even there and it isn't even included.

SCHOL-R-LEA

It is a dialect created by Microsoft and specific to their systems, which runs on the .Net CLR. It is really a new language, distinct from standard C++,

Is there no way to program standard C++ CLI applications without having this ".NET CLR"? To create C++ code that will work for general purpose, instead for Windows only? I did find Win32 > Win32 Console Application. I selected
Application type: Console Application
Additional options: [ ] Empty Project, [ ] Precompiled header, [X] Security Development Lifecycle checks.
Add common header files for: [ ] ATL, [ ] MFC.

After pressing "Finish", the code I got in return is:

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[]) {
    return 0;
}

Modified:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {

    const __int64 TheNumbah = 1844674407370955161;
    const string MarcoPolo = "Yupikayay!";
    cout << MarcoPolo << endl;
    cout << TheNumbah << endl;

    return 0;
}

Worked, thank you for your solutions everybody. Output:

Yupikayay!
1844674407370955161
Press any key to continue . . .

I've never used C++/CLI, but it looks like std::string is a distinct type from System::String and presumably Console::WriteLine only works with the latter as it is a .net method and std::string is a C++ class.

Correct. System::String^ is a managed reference type which is not directly compatible with std::string.

Presumably C++/CLI offers some way to convert between the two string types, but I imagine in this case it'd be easiest to just either only use C++ strings and C++ IO functions or .net strings and .net IO functions.

Yes, and agreed. There's a method to convert between the two, but it's awkward enough that it should be avoided where possible.

Results in: Error 1 error C1083: Cannot open include file: 'stdafx.h': No such file or directory. The funny answer I got is "just delete it"... but it's not even there and it isn't even included.

Visual Studio is stupid like that. If you generate a console project, stdafx.h will be generated as well, along with a switch in the project settings saying to expect it. For this reason I strongly recommend starting with an empty project instead, because it saves you from having to dig into project settings to fix things like this as well as delete any autogenerated kruft.

Is there no way to program standard C++ CLI applications without having this ".NET CLR"?

No.

Is there no way to program standard C++ CLI applications without having this ".NET CLR"? To create C++ code that will work for general purpose, instead for Windows only?

It is possible to write standard-compliant, cross-platform C++ code, though VS doesn't make that particularly easy for you. It is not possible to create C++/CLI applications that don't rely on .net because C++/CLI is a separate language from C++ and it's closely tied to .net.

Note that the "CLI" in "C++/CLI" stands for "Common Language Infrastructure", not "Command Line Interface". So if you want to create a command line application in C++, "C++/CLI" isn't the option you want to pick in the wizard (as you seem to have realized already).

Worked, thank you for your solutions everybody.

Note, that the code you have still won't compile with compilers other than VS's. To make it compile everywhere:

  • Disable pre-compiled headers in your project settings and remove the include of stdafx.h.
  • There is no type called __int64 in the C++ standard (or in any compiler besides VS's). From C++11 onwards you can use either long long (which will have at least 64 bits) or, if you need it to be exactly 64 bits, int64_t from the <cstdint> header. At least long long has been supported by the majority of compilers already long before C++11, so it shouldn't cause compatibility problems with older compilers.

PS: You don't need the <sstream> header for anything you did in your code.

The video tutorials that I'm following are led by a certain programmer, which uses XCode on iOS laptop/computer. He is able to write code and click on "Run", then little console shows up at the bottom of his editor, and makes printf("Hello!"); display "Hello", after the program has ended it says Program Ended with 0 code. (0x0), is there no such thing for Windows? I would install XCode if I could, but if I look it up, every time I see that one of the requirements is virtual machine.

is there no such thing for Windows?

It depends on the IDE being used and how it handles program output. I'd be surprised if there weren't an IDE that allows you to display output in a tool window like XCode. Typically you'd have your program open in a new console window for testing, is that not sufficient for your needs?

That said, you can do this with Visual Studio after a fashion.

  1. In VS, navigate to Tools | External Tools.
  2. Click the Add button.
  3. Under the Title field, type something like Console.
  4. Under the Command field, type cmd.exe.
  5. Under the Arguments field, type /c $(TargetPath)
  6. Check the Use Output window option.
  7. Hit OK to save the new tool.

Now Console is available from the Tools menu, and you can add a button to your toolbar that activates it easily to execute your latest build and redirect stdout to the Output window.

There may be an easier way to do this, but I don't know it.

Typically you'd have your program open in a new console window for testing, is that not sufficient for your needs?

Apparently it forces C++ to be .NET dependant because it's Win32 console application.

This is what I'm trying to avoid, I want to learn general C++, which would work on Windows and Linux, when provided exactly same code, so to see, only one I can write is C++/CLI for .NET, but I want to write general C++ code. Just to learn it, not distribute.

And I'll try to use the tricks you mentioned. Also, it takes SO LOOOONG...

@edit - Trick didn't work, it opens in cmd.exe, not in output window :|

Your programm will not be .net dependent if you choose "Win32 console application", only if you choose "C++/CLI".

The name "Win32 console application" may suggest that will depend on the Win32 API, but that's not actually true (unless you specifically use functions from the Win32 API).

Trick didn't work, it opens in cmd.exe, not in output window :|

It works for me. The "trick" might be version dependent. I tested it on Visual Studio CE 2013. You might also not have done something needed, like the output window check box in your external tool. I'm reasonably sure I listed all of the steps I followed.

You might also not have done something needed.

I did all the steps without an error. I don't think I forgot anything.

You might also not have done something needed, like the output window check box in your external tool.

Which "external tool" ? I did check the checkbox.

Which "external tool" ?

The one you created. That whole process was to make a new external tool to cmd.exe and link it to the output window.

I'm sorry, but I don't understand. Maybe I may ask a littlebit more assistance with this, as I would present what I've done with screenshots, is that okay?

So, I went to Tools > External Tools, then have set the dialogue according to your instructions. Then you told me about "external tool", which I created, but I've created none. Replacing $(TargetPath) with C:/.../ConsoleApplication3 or C:/.../ConsoleApplication3/ConsoleApplication3.exe didn't work.
$(TargetPath) also generated number of errors but then it vanished rapidly, all saying something about "PDO missing in SysWoW64".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.