WindowsRuntimeComponent runtime error - c++

I am having problems creating a managed class with namespace in visual studio 2012 Windows Runtime Component of C++.
Below is the code:-
#pragma once
#include <string>
using namespace std;
namespace WindowsRuntimeComponent1
{
public ref class Class1 sealed
{
public:
Class1();
string getString(string desc);
};
}
i'm getting error at 'public' which it expected a declaration.
beside that, exception return by visual studio 2012 is error C2059:syntax error:'public', error C2143:syntax error:missing ';' before '{', error C2447:'{':missing function header (old-style-formal list?)
Can anyone help me solve this problem. Thank you.

oYou have to change the runtime compiler. Go to Project -> Properties -> General and change it to "Common Language Runtime Support to /clr".
EDIT: Well, there is no need to worry about all the compiler errors. Google is your friend! if you don't know how to fix it, just look for it. All errors are described in detail. But most of the errors are self-explanatory, e.g. "error C4703, potentially uninitialized local pointer variable used" says you have to initialize your var int *xxx=0;.

Related

Problem compiling json with visual studio

I have another program that uses json but I do not manage to make it compile. I think that the problem is related to the library I am using to parse a Json file, so, I coded a small code to try to understand better how it works.
When I am compiling the program using visual Studio 2019 in Windows the following errors appear (I have translated the errors to english so some inaccuracy may have been introduced ):
Error C4996 'Json::Reader::parse': Use CharReader and CharReaderBuilder instead. ConsoleApplication5 C:\Users\S510U\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp Line:21
Warning C26812 The numeration type "Json::CommentPlacement" has no scope. Prefer "enum class" to "enum" (Enum.3). ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\packages\JsonCpp.Windows.1.9.2\build\native\include\json\value.h 589
Warning C26812 The numeration type "Json::ValueType" has no scope. Prefer "enum class" to "enum" (Enum.3). ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\packages\JsonCpp.Windows.1.9.2\build\native\include\json\value.h 618
Warning C26812 The numeration type "Json::PathArgument::Kind" has no scope. \UserName\source\repos\ConsoleApplication5\packages\JsonCpp.Windows.1.9.2\build\native\include\json\value.h 732
Waring C4275 It has been used an interface not of the DLL class 'std::exception' as a base for the interface DLL class 'Json::Exception' ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\packages\JsonCpp.Windows.1.9.2\build\native\include\json\value.h 57
Error C4996 'Json::Reader': Use CharReader and CharReaderBuilder instead. ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp 19
Error C4996 'Json::Reader::Reader': Use CharReader and CharReaderBuilder instead ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp 19
However, when I try to run it in Linux using g++ it works.
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <math.h>
#include "string.h"
#include <cstdlib>
#include "json/json.h"
using namespace std;
int main(void){
string namedir = "fichero.json";
ifstream ifs(namedir);
Json::Value root;
Json::Reader reader;
Json::Value obj;
reader.parse(ifs, obj);
return 0;
}
It seems like you are using an old interface of this library: https://github.com/open-source-parsers/jsoncpp ?
There is this error reported e.g. : https://github.com/open-source-parsers/jsoncpp/issues/815
This is a warning that says you should not use the old interface but instead the newer one. The warning is handled as an error from MSVC (visual studio) because your warning level is set to high. GCC does not throw an error because it lets pass the warning as it is (This is an assumption).
To resolve the warning you need to reduce the warning level in the project properties, but in my eyes this is a bad idea.
Better use the new interface of the library and always have a look at the warnings which the compiler is giving you. It will reduce a lot of headaches you coudl get while ignoring them ;) (like that problem)

Referring to an object from custom library gives error: "pointer to incomplete class type is not allowed"

So in my Visual Studio solution I'm making a library and I have two Visual Studio projects, one for the library and one for the sandbox. In the library I'm trying to use forward declarations to create a class. What I'm simply doing in this example is creating a header file for my class, declaring std::string with the following forward declaration and creating a member pointer with that class.
Library project:
ClassFromLibrary.h
#pragma once
namespace std {
class string;
}
class ClassFromLibrary {
public:
ClassFromLibrary();
~ClassFromLibrary();
std::string* forwardDeclaredString;
};
ClassFromLibrary.cpp
#include "ClassFromLibrary.h"
#include <string>
ClassFromLibrary::ClassFromLibrary()
: forwardDeclaredString(new std::string("Hello, world!"))
{
}
ClassFromLibrary::~ClassFromLibrary()
{
}
Sandbox project
main.cpp
#include <Library/ClassFromLibrary.h>
#include <iostream>
int main()
{
ClassFromLibrary test;
std::cout <<
*test.forwardDeclaredString //Root of the problem
<< std::endl;
std::cin.get();
}
The problem
As I said earlier, the library project compiles perfectly. However, the error which I mentioned in the title shows up when the forward declared member variable is referenced in any file from the sandbox project. I have a larger project where I get the same error, and the reason I want to achieve this is because I am using other external libraries within my library project, and when I create applications with it I don't want to have to put all the library include directories in the project properties, only the one for my library.
Thanks in advance!
You know that putting names in namespace std makes program ill-formed (except for some cases?)? Well, know you know why. The bug you have is a symptom of undefined behavior.
In my test, the way you declared your forward declaration in std is not how it is actually declared in string header. Yet it's a same name, so now you have name conflict (you have it as soon as you include iostream, which includes string. This is what my compiler is telling me when I am try compile your code:
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/basic_string.h:6628:17:
error: reference to 'string' is ambiguous
struct hash<string>
This is different from the error you put in the question, but since the behavior is undefined, anything can happen.

error C2976: 'std::tr1::array' : too few template arguments in MS C++ express 2010

I'm working on a Windows forms project in C++ Express 2010. Made some changes to the the Form.h and now get an error when compiling the program. Note the compiler suggests the error is in the main program - open gl test 2 - that #includes the form. All compiled fine for days as I developed the code, then changed something last night and now I get the error.
The main program in open gl test 2 is identical to that of any other forms based project I have created - I've compared to check [except for the obvious changes of namespace etc cause its a different project]
// open gl test 2.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace opengltest2;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
The error is:
1>open gl test 2.cpp(9): error C2976: 'std::tr1::array' : too few template arguments
1> D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\array(18) : see declaration of 'std::tr1::array'
1>open gl test 2.cpp(9): error C3699: '^' : cannot use this indirection on type 'std::tr1::array'
1> compiler replacing '^' with '*' to continue parsing
line that compiler is complaining about is
int main(array<System::String ^> ^args)
So the fault cant be in where the compiler claims it is - and I can not find a fault in the code in the included files - they compile correctly. I assume the error MUST be in form.h cause that's what I modified that causes this error, but I can not see it. And I'm pretty sure I did not change some project settings.
I ain't including the code for form1.h cause it's toooooo long. Guess what I'm looking for is peoples experience of searching for the error. What strategies can I use to resolve this? If I had hair to pull out I would be surrounded by it. Please help me save my non-existant hair.
It looks like the compiler is getting confused between the managed array type and the array type in the std library. This is probably because somewhere you're doing using namespace std - you should ideally explicitly reference the correct namespace (std::array or cli::array), or remove the include of <array> if it's not needed.
Note that the constant size std::array isn't suitable for use with command-line arguments - you'd need to use a variable size container like cli::array anyway.

Namespace compilation issues

I am new to working in Visual Studio (am using version 2005). I am running into a problem with namespaces that I am not able to figure out.
I am trying to create a static library which I will link to an Application later.
So, I have a XXX.h file with the following code
#ifndef _XXX_X_H
#define _XXX_X_H
namespace LLL_NWK
{
void lllInit();
}
#endif
I include XXX.h in XXX.c and the code looks like
#include "XXX.h"
using namespace LLL_NWK;
void lllInit()
{
}
However, when I build the Library I encounter the following errors
error C2061: syntax error : identifier 'LLL_NWK'
error C2059: syntax error : ';'
error C2449: found '{' at file scope (missing function header?)
error C2059: syntax error : '}'
I am unable to figure out the cause of this error. Would appreciate some help and pointers.
First, using namespace LLL_NWK is not appropriate here. You are declaring and defining a function void lllInit() outside of namespace LLL_NWK. You need to place the definition inside of the namespace, which can be done like this:
void LLL_NWK::lllInit()
{
}
or like this:
namespace LLL_NWK
{
void lllInit()
{
}
}
Second, make sure you compile the code as C++.
That code is not supported by the C compiler - makes sure to rename the filename to .cpp instead .c. In this case, namespace is not supported. See this post: Namespaces in C

Can't declare C++ vector in xcode ios project

I'm trying to use a vector in a C++ class with xcode but it's giving me errors. The file has the .mm extension that is required for C++ files.
This is my code:
class Synth{
private:
int bpm;
std::vector<Note> notesList;
public:
};
It's giving me these two errors:
error: Semantic Issue: Use of undeclared identifier 'std'
error: Parse Issue: Expected member name or ';' after declaration specifiers
I also tried with using namespace std; on top but that made no difference.
Any ideas what could be causing this?
Yes, you need to include the header:
#include <vector>
Don't use using namespace std in a header file, rather keep your code as it is, with the explicit qualifier: std::vector.