Stepping over the following code makes Visual Studio to fill up the RAM and then it crashes. Did I just made a terrible mistake, or this is a Visual Studio bug?
namespace d
{
class base
{};
template <typename Base>
class dummy : public Base
{
public:
dummy()
{}
};
}
class base : public d::base
{
public:
base()
{}
};
int main()
{
//d::dummy<base> dd;
using dummy_t = d::dummy<base>;
dummy_t dd2;
return 0;
}
If I uncomment the first line inside main the problem doesn't seem to appear. If I uncomment the first line and comment the following two then again, the problem doesn't appear. Also if I change the "base" class (which is derived from d::base) to something else the problem goes away.
Related
I am getting error code C2512 which is no appropriate default constructor available. However it appears to me that everything should be working so I am a bit confused on how to get this to compile.
Here is the header file code:
namespace MyNamespace
{
ref class ForwadDeclaredClass;
public ref class MyClass
{
public:
MyClass();
// ...
private:
ForwadDeclaredClass^ fc;
}
}
Now in my cpp file I define the forwarded class and try to use it in the constructor for MyClass.
using namespace MyNamespace;
//...
public ref class ForwardDeclaredClass
{
public:
ForwardDeclaredClass()
{
}
}
MyClass::MyClass()
{
// Compiler complains with error code here
fc = gcnew ForwardDeclaredClass();
}
I know I have to be missing something simple but I am just not seeing it. Any help would be appreciated.
At first glance, it looks like your namespaces are off.
In the header file, you're declaring ForwardDeclaredClass and MyClass inside MyNamespace.
In the cpp file, you're using MyNamespace, but the code you're writing isn't inside the namespace.
It looks like you need to enclose most of the contents of the cpp file in a namespace MyNamespace { block.
I have a class C which is derived from a generic class D. I referenced the class C in a XAML file. When I build the app, I get the following error:
The tag 'C' does not exist in XML namespace 'clr-namespace:A.B'
I don't understand is this due to C is derived from a generic class. If I remove the base class, it does not give me any error.
How can I get rid of the error? The IInterfaceForE is injected via Unity, so the app works fine. But I can't get rid of the build error.
Code snippet:
namespace A.B
{
public class C : D<InterfaceForE>
{
public C()
{
}
}
}
// Reference in XAML
xmlns:myns="clr-namespace:A.B"
<myns:C x:Key="KeyForC"/>
// Code snippet after commenting out the base class.
// When I build with this code, there is no build error, but the app wont run.
namespace A.B
{
public class C //: D<InterfaceForE>
{
public C()
{
}
}
}
I had to remove the generic being used to make this work.
I have a Page that imports controls from a library like this:
<Page
x:Class="Foo.Bar.SomePage"
xmlns:myNamespace="using:Bar.Controls">
<myNamespace:SomeControl x:Name="someControl">
<!-- snip -->
</myNamespace:SomeControl>
</Page>
As you can see here, the page is declared in the ::Foo::Bar namespace, while SomeControl is declared in the ::Bar namespace. The problem I face is that Visual Studio generates this code:
namespace Bar {
namespace Controls {
ref class SomeControl;
}
}
namespace Foo
{
namespace Bar
{
partial ref class SomePage : /* ... */
{
/* ... */
private: Bar::SomeControl^ someControl;
};
}
}
The field definition Bar::SomeControl^ someControl tries to select ::Foo::Bar::SomeControl instead of ::Bar::SomeControl because Visual Studio doesn't fully-qualify it.
Is this by design (is there a way to phrase the using: URI in such a way that it will fully-qualify the name), or is this a bug? How can I work around that?
I think that I could convince people to make an exception to the namespace structure for this specific class, but it would be much simpler if there was an in-code solution for this.
For posterity, right now I'm using this kludge before #including the g.h file, but it's not super pretty:
namespace Foo
{
namespace Bar
{
typedef ::Bar::SomeControl SomeControl;
}
}
It introduces the control into the (incorrect) namespace so that it works even though the XAML code generator gets it wrong.
I was wondering if it is possible to change the auto-formatting in Android Studio in such a way that braces for anonymous classes are placed on the same line while still putting braces for regular classes, methods and blocks on a new line.
Currently, IntelliJ is giving me this result:
class TestClass
{
public void test()
{
FooBar foo = new FooBar(new Runnable() // I want the brace here...
{ // ...not here.
#Override
public void run()
{
//
}
});
}
}
However, I would like the code to be formatted like this:
class TestClass
{
public void test()
{
FooBar foo = new FooBar(new Runnable() { // <- Same Line
#Override
public void run()
{
//
}
});
}
}
Everything is fine, except the one detail that I cannot get the brace to be formatted like in the second example. Is this not possible or did I just overlook the setting?
I've wanted this for a long time now as well, but unfortunately it is not possible in Intellij.
The closest you can come is setting Wrapping and Braces/Braces Placement/In class declaration to "Next line if wrapped" or "End of line" (what I use). This of course modifies the way the brace is wrapped for not only anonymous inner classes (the desired result), but also for top level and inner classes; however, methods/if/else/for/while/do-while/try/catch/finally/switch etc are unaffected.
I really wish IntelliJ would add a Wrapping and Braces/Braces Placement/In anonymous class declaration option like Eclipse has.
I'm programming in Visual C++ 2010. I've got a example.h example.cpp and Form1.h.
Basically, I've pasted bits of code. I'm not able to include Form1.h in the example.h file, not sure why. But The main question is how do I call Test (which is in form1.h) from example.cpp? What would be the syntax? Is it possible to do this?
My Form1.h
#include "example.h"
public ref class Form1 : public System::Windows::Forms::Form
{
public: void Test(void)
{
// Does something
}
}
My example.cpp
#include "example.h"
#include "Form1.h"
Test(); // would like to call Test from here.
You have two problems here:
You must call functions from inside of another function. The code you currently have in your example.cpp file is invalid, because you are trying to call the Test() function at global scope.
Make it look like this instead:
int main()
{
Test();
return 0;
}
This also solves the problem that you don't have a main function, which is the entry point to any C++ application.
More generally, I would strongly recommend using one of the project templates that comes with Visual Studio to get started (rather than copying and pasting random bits of code, like you said). That ensures that you have all of the things you need to get started, like an entry point. Once you have a solid foundation, you can start building up from there.
You might also find it useful to obtain either a book on C++/CLI or an online tutorial (such as this one: Hello C++/CLI, Part 1 and Hello C++/CLI, Part 2).
Your Test function is a member function of the Form1 class, which means that you need an object of that class in order to call it. Thus, the code should actually look like this:
int main()
{
Form1^ frm = gcnew Form1();
frm.Test();
return 0;
}
Alternatively, you could work around this by making the Test() function a static function. This would allow you to call it without having an instance of the class:
public ref class Form1 : public System::Windows::Forms::Form
{
public: static void Test(void)
{
// Does something
}
}
// ...
int main()
{
Form1::Test();
return 0;
}
However, beware that this means you cannot access any other members of the Form1 class inside of the Test() function (because there is no this pointer).
This should all be explained in whatever book/tutorial you decide to use to learn C++/CLI—search for a chapter about "Classes" or "Object-Oriented Design".