Con risposta VC++ 100% N00B question

  • martedì 13 marzo 2012 14:02
     
     

    Hey there,

    I am learning VC++ and trying to learn about reference types, but I keep getting this error: Can someone point me in the right direction?

    1>------ Build started: Project: TestCLR, Configuration: Debug Win32 ------

    1> MyClass.cpp

    1>MyClass.cpp(5): error C2533: 'MyClass::{ctor}' : constructors not allowed a return type

    1> TestCLR.cpp

    1>TestCLR.cpp(6): error C2143: syntax error : missing ';' before 'using'

    1> Generating Code...

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

     

    MyClass.h

    #pragma once

    Using namespace System;

    Ref class MyClass{

    Private:

    int m_Value;

    Public:

    MyClass(int);

    !MyClass();

    Int get_Value();

    };

    MyClass.cpp

    #include “stdafx.h”

    #include “MyClass.h”

    MyClass::MyClass(int x)

    { m_Value=x;

    }

    MyClass::!MyClass()

    {

           Console::WriteLine(“Class finalizing (deleted)”);

    }

    Int MyClass::get_Value(){ return m_Value;}

    RefType_2_Example.cpp

    #include “stadafx.h”

    #include “MyClass.h”

    Using namespace System;

    Int main(array<System::String^>^args)

    {

           MyClass ^m_Obj = gcnew MyClass(4);

           Console::WriteLine(m_Obj->get_Value());

           m_Obj= nullptr;

    GC::Collect();

    Console::WriteLine(“All done!”);

    Return 0;

    }

Tutte le risposte

  • martedì 13 marzo 2012 14:08
     
     

    CAAMSA wrote:

    I am learning VC++ and trying to learn about reference types, but I  keep getting this error: Can someone point me in the right
    direction?

    1>MyClass.cpp(5): error C2533: 'MyClass::{ctor}' : constructors not  allowed a return type

    MyClass.h
    Ref class MyClass{
    };

    Double-check that you do have this last semicolon in your actual code.  The error message suggests that you don't.


    Igor Tandetnik

  • martedì 13 marzo 2012 22:03
     
     Con risposta
    When you post code in these forums always use copy & paste
    to post the *actual* code you are trying to compile. Don't
    try to type it in again into a message, as that often
    results in changes that aren't in the real code.

    Some obvious errors that would cause a bunch of compile
    errors:

    >#include “stadafx.h”
    >#include “MyClass.h”
    >Console::WriteLine(“Class finalizing (deleted)”);
    >Console::WriteLine(“All done!”);

    All of these use the wrong double quote character.

    >Using namespace System;

    This is wrong. It should be

    using namespace System;

    >Ref class MyClass{

    This is wrong. It should be:

    ref class MyClass{

    >Private:

    This is wrong. It should be:

    private:

    >Public:

    This is wrong. It should be:

    public:

    etc.

    - Wayne
  • martedì 13 marzo 2012 22:08
     
     Con risposta
    Also, this:

    RefType_2_Example.cpp

    #include “stadafx.h”

    has the wrong file name: stadafx.h

    It should be: stdafx.h

    - Wayne
  • martedì 13 marzo 2012 22:11
     
     Con risposta
    And this:

    >Int get_Value();

    There is no type Int. It should be:

    int get_Value();

    When programming, accuracy is *vital*.
    You *must* be precise.

    - Wayne
  • martedì 13 marzo 2012 22:15
     
     Con risposta
    Some more:

    >Int main(array<System::String^>^args)

    Again, there is no type Int. It should be:

    int main(array<System::String^>^args)

    >Return 0;

    Wrong. Should be:

    return 0;

    - Wayne
  • giovedì 15 marzo 2012 03:11
    Moderatore
     
     Risposta suggerita Contiene codice

    Hi CAAMSA,

    I agree with WayneAKing. There are some typo in the code snippets you posted. I have created a CLR Console Application, and added a new class named MyClass into it. In the project, I tried your codes. Finally the project can run without errors. Please refer to the following codes for more information.
    MyClass.h file:

    #pragma once
    using namespace System;
    ref class MyClass
    {
    private: int m_Value;
    public:
    	MyClass(void);
    	MyClass(int);
    	!MyClass();
    	int getValue();
    };

    MyClass.cpp file:

    #include "StdAfx.h"
    #include "MyClass.h"
    
    MyClass::MyClass(void)
    {
    }
    MyClass::MyClass(int x)
    {
    	m_Value=x;
    }
    MyClass::!MyClass()
    {
    	Console::WriteLine("Class finalizing (deleted)");
    }
    int MyClass::getValue()
    {
    	return m_Value;
    }

    main.cpp file:

    #include "stdafx.h"
    #include "MyClass.h"
    using namespace System;
    
    int main(array<System::String ^> ^args)
    {
    	MyClass^ m_Obj=gcnew MyClass(4);
    	Console::WriteLine(m_Obj->getValue());
    	m_Obj=nullptr; 
    	GC::Collect();
    	Console::WriteLine("All done!");	
            return 0;
    }

    I hope you can find useful information from this reply. By the way, please mark the reply which helps you as answer.

    Thanks for your post in the MSDN Forum.
    Have a nice day!


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us