Thread: How do I instantiate a self written class inside a Windows Form?

  1. #1
    Registered User Megidolaon's Avatar
    Join Date
    Oct 2008
    Posts
    8

    Question How do I instantiate a self written class inside a Windows Form?

    I thought it would be fairly simple but no matter I do, I get tons of errors

    Code:
    class stuff
    {
    	int account;
    	string content;
    
    public: 
    
    	stuff()
    	{
    		account = 1;
    		content = "null";
    	}
    
    	stuff(int acc, string cont)
    	{
    		account = acc;
    		content = cont;
    	}
    	
    	~stuff(){};
    
    	int GetAccount()
    	{
    		return account;
    	}
    	
    	string GetContent()
    	{
    		return content;
    	}
    
    	void SetAccount(int Account)
    	{
    		account = Account;
    	}
    
    	void SetContent(string Content)
    	{
    		content = Content;
    	}
    };
    That's my humble class. All I'm trying to do is declare a variable of the type stuff .

    I inserted a new method under the public keyword in the code pre-created by Visual Studio:

    Code:
    void DoSomething()
    		{
    			int number = 119;
    			String ^name = "Something";
    
    			stuff s(211, "Nothing");
    
    		};
    When putting a parenthesis behind s, Visual Studio displays both available constructors for s but I still get the following errors:
    Error 1 error C2065: 'stuff' : undeclared identifier
    Error 2 error C2146: syntax error : missing ';' before identifier 's'
    Error 3 error C3861: 's': identifier not found
    Error 4 error C2065: 'stuff' : undeclared identifier
    Error 5 error C2146: syntax error : missing ';' before identifier 's'
    Error 6 error C3861: 's': identifier not found

    What went wrong?

    Thanks in advance.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> String ^name = "Something";

    What's with the stray '^'?

    >> stuff s(211, "Nothing");

    Be careful using constructors like that. In some cases, the compiler construes it as a function declaration! Rather:

    stuff s = stuff(211, "Nothing");
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sebastiani
    What's with the stray '^'?
    Probably C++/CLI.

    Quote Originally Posted by Sebastiani
    Be careful using constructors like that. In some cases, the compiler construes it as a function declaration! Rather:

    stuff s = stuff(211, "Nothing");
    No, it should be fine in this case, and in fact I would argue that it should be preferred to the copy initialisation that you showed. Frankly, I cannot duplicate the problem with the MinGW port of g++ 3.4.5 (after commenting out that C++/CLI line, at least).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    My guess is that the problem is related to this being C++/CLI. But that language is strange.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User Megidolaon's Avatar
    Join Date
    Oct 2008
    Posts
    8
    Thanks for the reply.

    What's with the stray '^'?
    System::String apparently needs it, I got more errors without that.

    My guess is that the problem is related to this being C++/CLI. But that language is strange.
    I see.
    Was a bad thing I combined C/C++ code with C/CLI code pre-generated by Visual Studio?

    I just tried again with some code from an SDK tutorial and it worked, kind of

    My main intention had been to first use a self made function in a windows forms application and I guess that worked, even though displaying some text in the title of the window didn't work the way I expected.

    I couldn't use a string and I have difficulties returning a char array (how would I do that?) and I can't seem to get the pointer/address combination right to use an already existing array as parameter and change it inside the function.

    PS: putting square brackets for arrays after the variable name instead of the type name confuses me every time >_>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM