CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2010
    Posts
    2

    C++ problems with class/default constructor

    Hey, I've been having issues trying to make a sample class program work. It uses two classes, "Bow" and "Test." Bow is included in Bow.h, and Bow.cpp is also included in the project files. Also, I'm trying to make it work as a CLR Console App.

    It compiles fine for me, but the output is wrong. When I run it, it should output like so, with random numbers for score:

    Round 1!
    The red bow has been drawn.
    The blue bow has been drawn.
    The red bow scored 5 points!
    The blue bow scored 4 points!
    Round 2!
    The red bow has been drawn.
    The blue bow has been drawn.
    The red bow scored 10 points!
    The blue bow scored 2 points!
    Red wins!

    However, the program seems to be calling the default constructor for my Bow class instead of the one I want it to, which should specify the color. It outputs with "The bow" instead of "The red bow" or "The blue bow." Here's the code.

    Bow.h:

    Code:
    #ifndef Bow_h
    #define Bow_h
    
    #include "stdafx.h"
    
    using namespace System;
    
    ref class Bow
    {
    	String^ color;
    	bool drawn;
    	int numOfArrows;
    
    public:
    
    	Bow(String^ scolor);
    	Bow(void);
    	~Bow();
    
    	void draw();
    	int fire();
    };
    
    #endif
    Bow.cpp:

    Code:
    #include "stdafx.h"
    #include "stdlib.h"
    #include "time.h"
    #include "Bow.h"
    
    Bow::Bow(String^ scolor)
    {
    	numOfArrows = 10;
    	drawn = false;
    	color = scolor;
    	
    	srand((unsigned)time(0));
    }
    Bow::Bow(void)
    {
    	numOfArrows = 10;
    	drawn = false;
        color = "";
    	srand((unsigned)time(0));
    }
    
    
    Bow::~Bow()
    {
    }
    
    void Bow::draw()
    {
    	drawn = true;
    	Console::WriteLine("The " + color + " bow has been drawn.");
    }
    
    int Bow::fire()
    {
    	if(!drawn)
    	{
    		Console::WriteLine("The " + color + " bow hasn't been drawn, and can't fire.");
    		return 0;
    	}
    	int score;
    	score = rand() % 11;
    	if (score == 0)
    	{
    		Console::WriteLine("The " + color + " bow has missed the target!");
    	}
    	else
    	{
    		Console::WriteLine("The " + color + " bow scored " + score + " points!");
    	}
    	drawn = false;
    	return score;
    }
    ultimatetest.cpp:

    Code:
    // ultimatetest.cpp : main project file.
    
    #include "stdafx.h"
    #include "Bow.h"
    
    using namespace System;
    
    ref class Test
    {
    public:
    	int rounds;
    	float redScore;
    	float blueScore;
     	Bow one;
    	Bow two;
    	
    public:
    	Test(int lrounds);
    	~Test();
    
    	int compete(void);
    };
    
    Test::Test(int lrounds)
    {
    	rounds = lrounds;
    	Bow one("red");
    	Bow two("blue");
    	redScore = 0;
    	blueScore = 0;
    }
    
    
    Test::~Test()
    {
    }
    
    int main(array<System::String ^> ^args)
    {
    	Bow red("red");
    	Bow blue("blue");
    	Test plymouthSquare(2);
    	plymouthSquare.compete();
    	return 0;
    }
    
    int Test::compete()
    {
    	for(int i = 0; i < rounds; i++)
    	{
    		Console::WriteLine("Round " + (i + 1) + "!");
    		one.draw();
    		two.draw();
    		redScore = (one.fire() + (redScore * i)) / (i + 1);
    		blueScore = (two.fire() + (blueScore * i)) / (i + 1);
    	}
    	if (redScore == blueScore)
    	{
    		Console::WriteLine("We have a tie!");
    	}
    	else if (redScore < blueScore)
    	{
    		Console::WriteLine("Blue wins!");
    	}
    	else
    	{
    		Console::WriteLine("Red wins!");
    	}
    	return 1;
    }
    I've had the feeling that the problem is caused by my use of managed classes, but I couldn't find any other way to declare a String^ as a data member in a class. I've checked up on my syntax and my call to the Bow constructor is correct if placed in main, but it appears to ignore the argument when used in the Test constructor. Any help would be greatly appreciated.

  2. #2
    Join Date
    Aug 2006
    Posts
    157

    Re: C++ problems with class/default constructor

    You declare Bow one and two as member variables of Test. Then in the constructor of Test you redeclare Bow one and two. Here you are declaring two new variables which will last for the scope of the constructor.

    Initialise your variables in the initialiser list as follows:
    Code:
    Test::Test(int lrounds): one("red"), two("blue")
    {
    	rounds = lrounds;
    	redScore = 0;
    	blueScore = 0;
    }

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ problems with class/default constructor

    Quote Originally Posted by nSpim View Post
    Hey, I've been having issues trying to make a sample class program work.
    That code is not C++.
    Code:
    ref class Bow
    {
    	String^ color;
    What is that "^" thing doing there? What is "ref"? This is not C++, but some other language (probably Managed C++, which is not C++).

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Feb 2010
    Posts
    2

    Re: C++ problems with class/default constructor

    Quote Originally Posted by sockman View Post
    You declare Bow one and two as member variables of Test. Then in the constructor of Test you redeclare Bow one and two. Here you are declaring two new variables which will last for the scope of the constructor.

    Initialise your variables in the initialiser list as follows:
    Code:
    Test::Test(int lrounds): one("red"), two("blue")
    {
    	rounds = lrounds;
    	redScore = 0;
    	blueScore = 0;
    }
    Thanks! It works as intended now, I appreciate the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured