I am at work on a gui based mergesort application. I am trying to print the steps as the recursive mergesort moves along. I am having trouble with accessing the "richTextBox1" component within my mergesort/print_arr() so I can print the current array out to the window. I am getting these errors which I understand a little bit. I think it has to do merge_sort being a static method trying to access a class component. I am stumped, any suggestions or workarounds for this without having to start completely over?
Here are the errors:
Form1.h(185): error C2227: left of '->Text' must point to class/struct/union/generic type
Form1.h(187): error C2227: left of '->Text' must point to class/struct/union/generic type
Form1.h(189): error C2227: left of '->Text' must point to class/struct/union/generic type
These errors are coming from the print_arr(arr[],size) method.
and the code......
#pragma once
#include <iostream>
#include <time.h>
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
namespace prog1 {
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace msclr::interop;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
static void randomize(int* a, int size){
srand ( time(NULL) );
for(unsigned i = 0; i < size; i++){
a[i] = rand()%45 + 1;
}
for(unsigned i = 0; i < size; i++){
cout << a[i] << " ";
}
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Button^ randButton;
private: System::Windows::Forms::Button^ increaseButton;
private: System::Windows::Forms::Button^ decreaseButton;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::Button^ clearButton;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->randButton = (gcnew System::Windows::Forms::Button());
this->increaseButton = (gcnew System::Windows::Forms::Button());
this->decreaseButton = (gcnew System::Windows::Forms::Button());
this->label2 = (gcnew System::Windows::Forms::Label());
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->clearButton = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(13, 13);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(65, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"Enter a size:";
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(84, 13);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(27, 20);
this->textBox1->TabIndex = 1;
//
// randButton
//
this->randButton->Location = System::Drawing::Point(118, 9);
this->randButton->Name = L"randButton";
this->randButton->Size = System::Drawing::Size(75, 23);
this->randButton->TabIndex = 2;
this->randButton->Text = L"Random";
this->randButton->UseVisualStyleBackColor = true;
this->randButton->Click += gcnew System::EventHandler(this, &Form1::randButton_Click);
//
// increaseButton
//
this->increaseButton->Location = System::Drawing::Point(200, 9);
this->increaseButton->Name = L"increaseButton";
this->increaseButton->Size = System::Drawing::Size(75, 23);
this->increaseButton->TabIndex = 3;
this->increaseButton->Text = L"Increasing";
this->increaseButton->UseVisualStyleBackColor = true;
//
// decreaseButton
//
this->decreaseButton->Location = System::Drawing::Point(282, 9);
this->decreaseButton->Name = L"decreaseButton";
this->decreaseButton->Size = System::Drawing::Size(75, 23);
this->decreaseButton->TabIndex = 4;
this->decreaseButton->Text = L"Decreasing";
this->decreaseButton->UseVisualStyleBackColor = true;
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(363, 14);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(91, 13);
this->label2->TabIndex = 5;
this->label2->Text = L"# of comparisons:";
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(16, 44);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(473, 238);
this->richTextBox1->TabIndex = 6;
this->richTextBox1->Text = L"";
//
// clearButton
//
this->clearButton->Location = System::Drawing::Point(411, 289);
this->clearButton->Name = L"clearButton";
this->clearButton->Size = System::Drawing::Size(75, 23);
this->clearButton->TabIndex = 7;
this->clearButton->Text = L"Clear";
this->clearButton->UseVisualStyleBackColor = true;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(501, 319);
this->Controls->Add(this->clearButton);
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->label2);
this->Controls->Add(this->decreaseButton);
this->Controls->Add(this->increaseButton);
this->Controls->Add(this->randButton);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"CS4413 MergeSort";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
static void print_arr(int* arr, int size){
richTextBox1->Text = "[ ";
for(int i = 0; i < size; i++){
richTextBox1->Text = arr[i] + ", ";
}
richTextBox1->Text = " ]";
}
static void merge_arrays(int h, int m, int arr[], int arrA[], int arrB[]){
int i = 0,j=0,k=0;
while(i < h && j < m){
if(arrA[i] < arrB[j]){
arr[k] = arrA[i];
i++;
}else{
arr[k] = arrB[j];
j++;
}
k++;
}
if(i > h){
for(int x = j; x < m; x++){
arr[k] = arrB[x];
k++;
}
}else{
for(int x = i; x < h; x++){
arr[k] = arrA[x];
k++;
}
}
}
static int* merge_sort(int* arr, const int size){
if ( size == 1 )
return arr;
int h = size/2;
int m = size/2;
int arrayAHsize = h;
int arrayBMsize = size - m;
//cout << "h: " << h << "arr[h]: " << arr[h] << "m: " << m << " arraryBMsize" <<
//arrayBMsize<< endl;
int *arrA = (int*)malloc(h);
int *arrB = (int*)malloc(arrayBMsize);
int* pa;
int* pb;
for(int i = 0; i < h; i++){
arrA[i] = arr[i];
}
for(int i = 0; i < arrayBMsize; i++){
arrB[i] = arr[i + h];
}
cout << endl;
print_arr(arrA, size/2);
cout << "----";
print_arr(arrB, arrayBMsize);
cout << endl;
//l1 = mergesort( l1 )
pa = merge_sort(arrA,h);
//l2 = mergesort( l2 )
pb = merge_sort(arrB, arrayBMsize);
merge_arrays( h, arrayBMsize,arr, arrA, arrB);
}
private: System::Void randButton_Click(System::Object^ sender, System::EventArgs^ e) {
String^ s = textBox1->Text;
string Ssize = marshal_as<std::string>(s);
const int size = atoi(Ssize.c_str());
//int a[atoi(Ssize.c_str())];
int *a = (int*)malloc(size);
int* pa = a;
//int* pa;
randomize(a, size);
richTextBox1->Text += "Your set: \n";
for(int i = 0; i < size; i++){
richTextBox1->Text += a[i] + ", ";
}
pa = merge_sort(a,size);
}
};
}
static void print_arr(int* arr, int size){
richTextBox1->Text = "[ ";
for(int i = 0; i < size; i++){
richTextBox1->Text = arr[i] + ", ";
}
richTextBox1->Text = " ]";
}
Is wrong because richTextBox1 is a data member of the class, and a static function cannot access a class data member. Data members are for instances of the class, and thus a static function, not having an instance, would not know which instances data members to refer to.
Make this method non-static and you'll be fine.
Related
I was creating a custom operating system and I have a problem. I have an array of child components in an array of all opened windows. When I call a function CDraw (Component Draw) on the child array the system crashes with the message "General Protection Fault Detected".
Window array and code:
int openedWindows = 0;
Window* windows[128];
int OpenWindow(Window win) {
windows[0][openedWindows] = win;
openedWindows++;
return openedWindows-1;
};
void CloseWindow(int index) {
for(int i = index+1; i < 127; i++) {
if(i != 127)
windows[0][i] = windows[0][i-1];
else
windows[0][i] = Window();
}
openedWindows--;
};
void CloseAllWindows() {
for(int i = 0; i < 127; i++) {
windows[0][i] = Window();
}
openedWindows = 0;
};
/*Draw Windows*/
for(int i = 0; i < openedWindows; i++)
{
windows[0][i].Update();
}
Where I call the code:
Window wnd = Window("Window");
wnd.Update();
Button btn = Button(wnd, "Hello");
btn.CPosition = Point(10, 10);
btn.BackColor = Color(MediumTurquoise);
btn.parent = &wnd;
btn.parent->AddComponent(btn);
btn.Update();
Button btn2 = Button(wnd, "H2");
btn2.CPosition = Point(100, 100);
btn2.BackColor = Color(Red);
btn2.parent = &wnd;
btn2.parent->AddComponent(btn2);
btn2.Update();
OpenWindow(wnd);
Componet code:
int childIndex = 0;
BaseGuiComponent* children[128];
int BaseGuiComponent::AddComponent(BaseGuiComponent baseGuiComponent) {
children[0][childIndex] = baseGuiComponent;
childIndex++;
return childIndex-1;
};
void BaseGuiComponent::RemoveComponent(int index) {
for(int i = index+1; i < 127; i++) {
if(i != 127)
children[0][i] = children[0][i-1];
else
children[0][i] = BaseGuiComponent();
}
childIndex--;
};
void BaseGuiComponent::ClearComponents() {
for(int i = 0; i < 127; i++) {
children[0][i] = BaseGuiComponent();
}
childIndex = 0;
};
//List components and Update();
void BaseGuiComponent::DrawChildren() {
for(int i = 0; i < childIndex; i++) {
children[0][i].Update();
}
};
Drawing code:
void BaseGuiComponent::CDraw() {
if (strcmp(type,"Window",128)) {
Draw->DrawWindow(BackColor.GetHexColor(), CPosition.X, CPosition.Y, CSize.Width, CSize.Height, 2, CText, WindowStyle, Draw->GetScreenBounds());
DrawChildren();
} else if (strcmp(type,"Button",128)) {
Draw->FillRect(BackColor.GetHexColor(), CGlobalPosition.X*4, CGlobalPosition.Y, CSize.Width*4, CSize.Height, parent->bound/* Conflicting code */);
Draw->PRINTAT(ForeColor.GetHexColor(),CText, CGlobalPosition.X + nabs(CSize.Width - svlen(CText))/2,CGlobalPosition.Y + nabs(CSize.Height-16)/2, bound);
}
};
All help is welcomed. Thanks.
I'm trying to concatenate two arrays and at the end concatenate int, for example: result = arg + arg + 2;
I'm getting "read access violation" at + operator overloading.
I wrote the error and warning in comments below.
My code:
Main:
#include <iostream>
#include <string>
#include "CTable.h"
int main() {
CTable c_tab1, c_tab0;
c_tab0.SetNewSize(3);
c_tab1.SetNewSize(2);
c_tab0.SetValueAt(0, 22);
c_tab0.SetValueAt(1, 23);
c_tab0.SetValueAt(2, 24);
c_tab0.Print();
c_tab1.SetValueAt(0, 31);
c_tab1.SetValueAt(1, 32);
c_tab1.Print();
CTable c_tab3 = (c_tab0 + c_tab1 + 111);
c_tab3.Print();
return 0;
}
Class CTable:
#include <iostream>
#include <string>
using namespace std;
class CTable {
public:
CTable();
CTable(string sName, int iTableLen);
CTable(const CTable& pcOther);
CTable* pcClone();
~CTable();
void ShowName();
void ShowSize();
void SetName(string sName);
bool SetNewSize(int iTableLen);
void SetValueAt(int iOffset, int iNewVal);
void Print();
CTable& operator+(const CTable& pcNewTable);
CTable operator+(int iNewVal) const;
CTable& operator=(const CTable& pcNewVal) {
if (this != &pcNewVal) {
for (int i = 0; i < i_size; i++) {
this->piTable[i] = pcNewVal.piTable[i];
}
}
return *this;
}
private:
string s_name;
int i_size;
int* piTable;
const int SIZE = 10;
const string NAME = "Name";
};
#include <iostream>
#include <string>
#include "CTable.h"
#include <algorithm>
using namespace std;
CTable::CTable() {
s_name = NAME;
cout << "bezp: " << s_name << endl;
piTable = new int[SIZE];
i_size = SIZE;
}
CTable::CTable(string sName, int iTableLen) {
s_name = sName;
cout << "parametr: " << sName << endl;
piTable = new int[iTableLen];
i_size = iTableLen;
}
CTable::CTable(const CTable& pcOther) {
s_name = pcOther.s_name + "copied";
piTable = new int[pcOther.i_size];
i_size = pcOther.i_size;
for (int i = 0; i < pcOther.i_size; i++) {
piTable[i] = pcOther.piTable[i];
}
}
CTable::~CTable() {
delete[] piTable;
}
void CTable::SetName(string sName) {
s_name = sName;
}
bool CTable::SetNewSize(int iTableLen) {
if (iTableLen <= 0) {
cout << "Length has to be greater than 0" << endl;
return false;
}
int* pi_newTable = new int[iTableLen];
for (int i = 0; i < iTableLen; i++) {
pi_newTable[i] = piTable[i];
}
delete this->piTable;
this->i_size = iTableLen;
this->piTable = pi_newTable;
return true;
}
CTable* CTable::pcClone() {
CTable* ct = new CTable(s_name, i_size);
return ct;
}
void CTable::ShowName() {
cout << "Name: " << s_name << endl;
}
void CTable::ShowSize() {
cout << "Size: " << i_size << endl;
}
void CTable::SetValueAt(int iOffset, int iNewVal) {
if (iOffset >= this->i_size) {
return;
}
piTable[iOffset] = iNewVal;
}
void CTable::Print() {
for (int i = 0; i < i_size; i++) {
cout << piTable[i] << " ";
}
cout << endl;
}
CTable& CTable::operator+(const CTable& pcNewTable) {
CTable result("new_int", this->i_size);
result.i_size = (i_size + pcNewTable.i_size);
result.piTable = new int[i_size + pcNewTable.i_size];
for (int i = 0; i < i_size; i++) {
result.piTable[i] = piTable[i];
}
for (int i = 0; i < (pcNewTable.i_size); i++) {
result.piTable[i+i_size] = pcNewTable.piTable[i];
}
return result; //Warning C4172 returning address of local variable or temporary: result
}
CTable CTable::operator+(int iNewVal) const {
CTable result("new_int", this->i_size);
result.i_size = (i_size + 1);
result.piTable = new int[i_size + 1];
for (int i = 0; i < i_size; i++) {
result.piTable[i] = piTable[i]; //Exception thrown: read access violation. **this->piTable** was 0x1110122.
}
result.piTable[i_size + 1] = iNewVal;
return result;
}
What should I correct? I'm not sure about assigment operator overload, is it okay?
The member function SetNewSize has undefined behavior. In this loop
int* pi_newTable = new int[iTableLen];
for (int i = 0; i < iTableLen; i++) {
pi_newTable[i] = piTable[i];
}
it 1) uses uninitialized values because the array was not initialized and 2) iTableLen can be gretaer than the current value of i_size. You should at least zero initialize the array in constructors.
The copy assignment operator aslo has undefined behabior because the number of elements of the array of the object pcNewVal can be less than the number of elements of the array in the assigned object.
The first overloaded operator + also have undefined behavior. For starters as the warning says the operator returns reference to the local object result that will not be alive after exiting the operator. Secondly, there is a memory leak necause the array of the object is allocated anew and the previuous allocated memory in the constructor is not freed.
CTable result("new_int", this->i_size);
result.i_size = (i_size + pcNewTable.i_size);
result.piTable = new int[i_size + pcNewTable.i_size];
//...
The second overloaded operator + also has undefined behavior. As in the previous operator there is a memory leak.
In this statement
result.piTable[i_size + 1] = iNewVal;
there is an access memory outside the allocated array. There should be
result.piTable[i_size] = iNewVal;
Honestly, I have no idea why CTable cannot convert to CTable? I'm trying to make overload operator + which returns concatenated two tables.
Error C2440 'initializing': cannot convert from 'CTable' to 'CTable'
Second error: Error (active) E0334 class "CTable" has no suitable copy constructor
#include <iostream>
#include <string>
#include "CTable.h"
int main() {
CTable c_tab_1, c_tab_0;
c_tab_0.vSetValueAt(0, 1);
c_tab_0.vPrint();
c_tab_0.bSetNewSize(4);
c_tab_0.vPrint();
c_tab_1.bSetNewSize(4);
c_tab_0.vSetValueAt(0, 1);
c_tab_0.vSetValueAt(1, 2);
c_tab_0.vSetValueAt(2, 3);
c_tab_0.vSetValueAt(3, 4);
c_tab_0.vPrint();
c_tab_1.vSetValueAt(0, 2);
c_tab_1.vSetValueAt(1, 3);
c_tab_1.vSetValueAt(2, 4);
c_tab_1.vSetValueAt(3, 5);
c_tab_1.vPrint();
c_tab_1.vSetValueAt(2, 123);
c_tab_0.vPrint();
c_tab_1.vPrint();
CTable c_tab_3 = c_tab_0 + c_tab_1; //Error C2440! <-----------------------
c_tab_3.vPrint();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class CTable {
public:
CTable();
CTable(CTable& pcOther);
CTable* pcClone();
~CTable();
bool bSetNewSize(int iTableLen);
void vSetValueAt(int iOffset, int iNewVal);
void vPrint();
CTable operator+(const CTable& pcNewTable);
private:
string s_name;
int i_size;
int* piTable;
const int SIZE = 10;
const string NAME = "Name";
};
#include <iostream>
#include <string>
#include "CTable.h"
using namespace std;
CTable::CTable() {
s_name = NAME;
cout << "bezp: " << s_name << endl;
piTable = new int[SIZE];
i_size = SIZE;
}
///
CTable::CTable(CTable& pcOther) {
s_name = pcOther.s_name + "_copy";
piTable = new int[pcOther.i_size];
i_size = pcOther.i_size;
for (int i = 0; i < pcOther.i_size; i++) {
piTable[i] = pcOther.piTable[i];
}
cout << "kopiuj: " << s_name << endl;
}
CTable::~CTable() {
delete[] piTable;
cout << "usuwam " << s_name << endl;
}
////
bool CTable::bSetNewSize(int iTableLen) {
if (iTableLen <= 0) {
return false;
}
int* pi_newTable = new int[iTableLen];
memcpy(pi_newTable, piTable, iTableLen);
delete this->piTable;
this->i_size = iTableLen;
this->piTable = pi_newTable;
return true;
}
///
void CTable::vSetValueAt(int iOffset, int iNewVal) {
if (iOffset >= this->i_size) {
return;
}
piTable[iOffset] = iNewVal;
}
void CTable::vPrint() {
for (int i = 0; i < i_size; i++) {
cout << piTable[i] << " ";
}
cout << endl;
}
CTable CTable::operator+(const CTable& pcNewTable) {
CTable result;
int greater_len = i_size < pcNewTable.i_size;
int smaller_len = i_size > pcNewTable.i_size;
int* greater_table = i_size > pcNewTable.i_size ? piTable : pcNewTable.piTable;
int* smaller_table = i_size <= pcNewTable.i_size ? piTable : pcNewTable.piTable;
result.i_size = greater_len;
result.piTable = new int[greater_len];
memcpy(result.piTable, greater_table, greater_len * sizeof(int));
for (int i = 0; i < smaller_len; i++) {
result.piTable[i] += smaller_table[i];
}
return result;
}
What should I correct?
Try to define
CTable(const CTable& pcOther)
{
.......
}
when I try to set
cub.SetArray(cube);
I get an error
Console Application1.exe has triggered a breakpoint
What I'm doing wrong? When I try to debug cub -> cubesarray I get size -842150451. I don't understand why.Here's my all code
class Cube{
public:
static const int Change_ARRAY = 5;
private:
string color;
int size;
int *walls;
int n; // current size of array
int maximumsize; // maximum size of array
void Increase(int many);
public:
Cube(int maximumsize = 0);
~Cube();
void SetWalls(int wall);
void SetColor(string color);
void SetSize(int size);
string GetColor(){return color;}
int GetWalls(int i){return walls[i];}
int GetSize(){return size;}
int GetN(){return n;}
};
Cube::Cube(int maximumsize):n(0), maximumsize(maximumsize), size(size), walls(NULL){
if(maximumsize > 0){
walls = new int[maximumsize];
}
}
Cube::~Cube(){
if(walls){
delete [] walls;
}
}
void Cube::Increase(int many){
if(many > maximumsize){
int *newest = new int[many];
for(int i=0; i<n; i++)
newest[i] = walls[i];
delete [] walls;
walls = newest;
maximumsize = many;
}else if( many < maximumsize){
int *newest = new int[many];
for(int i=0; i<many; i++)
newest[i] = walls[i];
delete [] walls;
walls = newest;
n = maximumsize = many;
}
}
void Cube::SetWalls(int wall){
if(n == maximumsize) Increase(n + Change_ARRAY);
walls[n] = wall;
n++;
}
void Cube::SetColor(string color){
this->color = color;
}
void Cube::SetSize(int size){
this->size = size;
}
class CubesArray{
public:
static const int Change_Array = 5;
private:
Cube *cubesarray;
int currentsize; // current size of array
int maxsize; // maximumsize
void Change (int kk);
public:
CubesArray(int maxsize = 1);
~CubesArray();
void SetArray(Cube c);
Cube GetArray(int ind){return cubesarray[ind];}
int GetCsize(){return currentsize;}
};
CubesArray::CubesArray(int maxsize):cubesarray(NULL), currentsize(0), maxsize(maxsize){
if(maxsize > 0){
cubesarray = new Cube[maxsize];
}
}
CubesArray::~CubesArray(){
if(cubesarray){
delete [] cubesarray;
}
}
void CubesArray::Change(int kk){
if(kk > maxsize){
Cube *newarr = new Cube[kk];
for(int i=0; i<currentsize; i++)
newarr[i] = cubesarray[i];
delete [] cubesarray;
cubesarray = newarr;
maxsize = kk;
}if(kk < maxsize){
Cube *newarr = new Cube[kk];
for(int i=0; i<kk; i++)
newarr[i] = cubesarray[i];
delete [] cubesarray;
cubesarray = newarr;
currentsize = maxsize = kk;
}
}
void CubesArray::SetArray(Cube cub){
if(currentsize = maxsize) Change(currentsize + Change_Array);
cubesarray[currentsize] = cub;
currentsize++;
}
void Read(CubesArray & cub);
int main(){
CubesArray cub;
Read(cub);
system("pause");
return 0;
}
void Read(CubesArray & cub){
string color;
int size;
int i=0;
Cube cube;
ifstream fd(Data);
while(!fd.eof()){
fd >> color >> size;
cube.SetSize(size);
cube.SetColor(color);
cout << cube.GetColor() << " " << cube.GetSize() << " ";
while(fd.peek() != '\n' && !fd.eof()){
int w;
fd >> w;
cube.SetWalls(w);
cout << cube.GetWalls(i) << " ";
cub.SetArray(cube); // when I set cube to cub I get this error!!!
i++;
}
cout << endl;
fd.ignore();
}
}
Change:
if(currentsize = maxsize)
To:
if(currentsize == maxsize)
In addition, here is your real problem:
You have no copy-constructor in class Cube, so the walls array is not properly copied whenever you send a Cube instance by value, e.g., cub.SetArray(cube).
You must define it as follows:
Cube::Cube(const Cube& cube):n(cube.n),maximumsize(cube.maximumsize),size(cube.size),wall(NULL)
{
if (maximumsize > 0)
{
walls = new int[maximumsize];
for (int i=0; i<maximumsize; i++)
wall[i] = cube.wall[i];
}
}
And you have no assignment-operator in class Cube, so the walls array is not properly copied whenever you assign one Cube instance into another, e.g., cubesarray[currentsize] = cub.
You must define it as follows:
Cube& Cube::operator=(const Cube& cube)
{
n = cube.n;
maximumsize = cube.maximumsize;
size = cube.size;
wall = NULL;
if (maximumsize > 0)
{
walls = new int[maximumsize];
for (int i=0; i<maximumsize; i++)
wall[i] = cube.wall[i];
}
return *this;
}
BTW, in the copy-constructor, you can simply call the assignment-operator (remove coding redundancy):
Cube::Cube(const Cube& cube)
{
if (this != &cube)
*this = cube;
}
Your Cube class violates the rule of three. Look here:
void CubesArray::SetArray(Cube cub){ // calls copy constructor
That call creates a copy of your Cube class. Your Cube class is not safely copyable. Please see this and scroll down to the Managing Resources section: What is The Rule of Three?
You should pass Cube by reference or const reference, not by value. Doing so may correct the error you're having now, but still, your class is faulty.
So what im trying to do is use an array of pointers to keep track of spawning bots and let them do battle by colliding. This is done by using methods in an array of pointers but i keep getting a LNK2020 error when I build it
This is the VBot.h File
class VBot
{
public:
VBot( int startX, int startY, Panel ^ drawingPanel ) :
x(startX), y(startY), panel(drawingPanel), energy(100), image(NULL) { };
virtual ~VBot() { };
virtual void Move() = 0;
virtual int EnergyToFightWith() = 0;
bool IsDead() const { return energy <= 0; }
virtual void Show();
bool CollidedWith ( VBot * b ) const;
void DoBattleWith ( VBot * b );
protected:
int x, y; // Current position of the VBot
gcroot<Drawing::Bitmap ^> image; // Image displayed for the VBot
gcroot<Panel ^> panel; // Panel on which to show the VBot.
int energy;
VBot();
};
class CNNBot : public VBot
{
public:
CNNBot( int startX, int startY, Panel ^ drawingPanel ):
VBot(startX, startY, drawingPanel)
{
image = gcnew Drawing::Bitmap("HappyBot.bmp");
}
~CNNBot(){};
void Move();
int EnergyToFightWith();
bool IsDead() { return (VBot::IsDead()); }
virtual void Show() { VBot::Show();}
bool CollidedWith ( VBot * b ) { return VBot::CollidedWith(b);}
void DoBattleWith ( VBot * b ){ VBot::DoBattleWith(b);}
private:
static const int MOVE_VAL = 55;
static const int RIGHT_BOUND = 490;
static const int DOWN = 40;
static const int MAXY = 379;
bool switcher;
};
This is the VBot.cpp File
#include "stdafx.h"
#include "Vbot.h"
void VBot::Show()
{
Graphics ^ g = panel->CreateGraphics();
g->DrawImageUnscaled( image, x, y );
g->~Graphics();
}
bool VBot::CollidedWith ( VBot * b ) const
{
if ( b == NULL )
return false;
return ( x + image->Width ) >= b->x
&& ( b->x + b->image->Width ) >= x
&& ( y + image->Height ) >= b->y
&& ( b->y + b->image->Height ) >= y;
}
void VBot::DoBattleWith ( VBot * b )
{
int mine = EnergyToFightWith();
int yours = b->EnergyToFightWith();
if( mine == yours )
{
energy = energy - mine / 2;
b->energy = b->energy - yours / 2;
}
void CNNBot::Move()
{
if (this->switcher)
{
this->x += MOVE_VAL;
if( this->x >= RIGHT_BOUND)
{
this->x = 0;
this->y += DOWN;
if(this->y > MAXY)
{
this->switcher = false;
this->y = MAXY;
}
}
}
else
{
this->x += MOVE_VAL;
if( this->x >= RIGHT_BOUND)
{
this->x = 0;
this->y -= DOWN;
if(this->y < 0)
{
this->switcher = true;
this->y = 0;
}
}
}
panel->Invalidate();
}
This is the BotContainer.h
#include <vcclr.h>
#include "VBot.h"
#include "stdafx.h"
#ifndef BOTCONTAINER_H
#define BOTCONTAINER_H
using namespace std;
//------------------------------------------------------------------------------
// This class is an array class that will contain pointers to bots that are spawned
//
class BotContainer
{
private:
static const int MAXARRAY = 1000; //this is the max size of the array
VBot * list[MAXARRAY]; //This is the array of pointers
int count; // the variable that keeps track of the array
public:
//-------------------------------------------------------------------------------
//this the constructor that sets count to zero
//-------------------------------------------------------------------------------
BotContainer(){ count = 0;}
//--------------------------------------------------------------------------------
//This method will check for a collision between the all possible combinations of
//two bots it is const
//---------------------------------------------------------------------------------
void checkCollisions() const;
//---------------------------------------------------------------------------------
//This method will destroy all dead bots in the array
//---------------------------------------------------------------------------------
void destroy();
//----------------------------------------------------------------------------------
//This method will take given bot pointer and add it to the list.
//The Parameters are a pointer to a VBot called inBot.
//This inBot will be added to the end of the array which will then increment count
//Before adding it checks to make sure the array isn't full.
//----------------------------------------------------------------------------------
void add(VBot * inBot);
void Add(CNNBot * inBot);
//----------------------------------------------------------------------------------
//This method will move all the elements in the array.
//----------------------------------------------------------------------------------
void moveAll();
};
#endif
This the BotContainer.cpp file
#include "stdafx.h"
#include <vcclr.h>
#include "BotContainer.h"
//---------------------------------------------------------------------------------
//This method will delete all the deat bots in the array from the highest indexed
//dead bot. If a bot is deleted then the method will shift all higher indexed elements
// to one index less than its previous index
//-------------------------------------------------------------------------------------
void BotContainer::destroy()
{
for( int i = count - 1; i <= 0; i--)
{
if ( list[i]->IsDead() == true )
{
int current = i; //index of the dead array
delete list[i];
for( int j = i + 1; j < count; j++)
{
list[current] = list[j];
current = j; // index of the next element above the one just shifted
}
count--;
}
}
}
//------------------------------------------------------------------------------------
//This method will go throught the array calling move() for each bot in the array
//-----------------------------------------------------------------------------------
void BotContainer::moveAll()
{
for( int i = 0; i < count; i++)
{
list[i]->Move();
}
}
//-----------------------------------------------------------------------------------
//This method checks for collisions between any 2 bots in the array making sure to not
//test (ivsi) or (ivsj) and also testing (jvsi).
// This method is const therfore it doesn't change the array just tests it.
//-----------------------------------------------------------------------------------
void BotContainer::checkCollisions() const
{
for( int i = 0; i < count; i++)
{
if (list[i]->IsDead() != true)
{
for( int j = i + 1; j < count; j++)
{
if(list[j]->IsDead() != true)
{
if ( list[i]->CollidedWith(list[j]))
{
list[i]->DoBattleWith(list[j]);
}
}
}
}
}
}
//--------------------------------------------------------------------------------
//This method will add a pointer of a bot to the array checking first to make sure
//the array isn't full
//--------------------------------------------------------------------------------
void BotContainer::add(VBot * inBot)
{
if(this->count <= MAXARRAY)
{
list[count] = inBot;
count++;
}
}
void BotContainer::Add(CNNBot * inBot)
{
if(this->count <= MAXARRAY)
{
list[count] = inBot;
count++;
}
}
And this is the Form.h file
#pragma once
#include "stdafx.h"
#include <vcclr.h>
#include "BotContainer.h"
namespace Prog3
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
x = 0;
y = 0;
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Panel^ pnlGameZone;
private: System::Windows::Forms::ComboBox^ cmbSelect;
protected:
private: System::Windows::Forms::Button^ btnBotSpawn;
private: System::Windows::Forms::TrackBar^ trackBar1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
int x;
int y;
BotContainer VList();
private: System::Windows::Forms::Timer^ tmrspeed;
private: System::Windows::Forms::NumericUpDown^ updownX;
private: System::Windows::Forms::NumericUpDown^ updownY;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->pnlGameZone = (gcnew System::Windows::Forms::Panel());
this->cmbSelect = (gcnew System::Windows::Forms::ComboBox());
this->btnBotSpawn = (gcnew System::Windows::Forms::Button());
this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->updownX = (gcnew System::Windows::Forms::NumericUpDown());
this->updownY = (gcnew System::Windows::Forms::NumericUpDown());
this->tmrspeed = (gcnew System::Windows::Forms::Timer(this-
>components));
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->trackBar1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->updownY))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->updownY))->BeginInit();
this->SuspendLayout();
//
// pnlGameZone
//
this->pnlGameZone->BackColor =
System::Drawing::SystemColors::Window;
this->pnlGameZone->Location = System::Drawing::Point(53, 102);
this->pnlGameZone->Name = L"pnlGameZone";
this->pnlGameZone->Size = System::Drawing::Size(495, 379);
this->pnlGameZone->TabIndex = 0;
//
// cmbSelect
//
this->cmbSelect->DropDownStyle =
System::Windows::Forms::ComboBoxStyle::DropDownList;
this->cmbSelect->FormattingEnabled = true;
this->cmbSelect->Items->AddRange(gcnew cli::array< System::Object^
>(3) {L"CNN Bot\t", L"MSNBC Bot", L"FOX NEWS Bot"});
this->cmbSelect->Location = System::Drawing::Point(53, 52);
this->cmbSelect->Name = L"cmbSelect";
this->cmbSelect->Size = System::Drawing::Size(121, 21);
this->cmbSelect->TabIndex = 1;
//
// btnBotSpawn
//
this->btnBotSpawn->Location = System::Drawing::Point(427, 48);
this->btnBotSpawn->Name = L"btnBotSpawn";
this->btnBotSpawn->Size = System::Drawing::Size(87, 23);
this->btnBotSpawn->TabIndex = 4;
this->btnBotSpawn->Text = L"Spawn a Bot";
this->btnBotSpawn->UseVisualStyleBackColor = true;
this->btnBotSpawn->Click += gcnew System::EventHandler(this,
&Form1::tmrspeed_Tick);
//
// trackBar1
//
this->trackBar1->Location = System::Drawing::Point(552, 48);
this->trackBar1->Maximum = 800;
this->trackBar1->Minimum = 50;
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size = System::Drawing::Size(134, 45);
this->trackBar1->TabIndex = 5;
this->trackBar1->TickFrequency = 100;
this->trackBar1->Value = 50;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(50, 34);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(65, 13);
this->label1->TabIndex = 6;
this->label1->Text = L"Select a Bot";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(200, 35);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(53, 13);
this->label2->TabIndex = 7;
this->label2->Text = L"Starting X";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(306, 35);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(53, 13);
this->label3->TabIndex = 8;
this->label3->Text = L"Starting Y";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(549, 32);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(103, 13);
this->label4->TabIndex = 9;
this->label4->Text = L"Speed of Movement";
//
// updownX
//
this->updownX->Location = System::Drawing::Point(203, 53);
this->updownX->Name = L"updownX";
this->updownX->Size = System::Drawing::Size(85, 20);
this->updownX->TabIndex = 12;
//
// updownY
//
this->updownY->Location = System::Drawing::Point(309, 52);
this->updownY->Name = L"updownY";
this->updownY->Size = System::Drawing::Size(80, 20);
this->updownY->TabIndex = 13;
//
// tmrspeed
//
this->tmrspeed->Tick += gcnew System::EventHandler(this,
&Form1::tmrspeed_Tick);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(740, 493);
this->Controls->Add(this->updownY);
this->Controls->Add(this->updownX);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->trackBar1);
this->Controls->Add(this->btnBotSpawn);
this->Controls->Add(this->cmbSelect);
this->Controls->Add(this->pnlGameZone);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->trackBar1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->updownX))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >e) e)
(this->updownY))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void btnBotSpawn_Click(System::Object^ sender, System::EventArgs^
e) {
if ( cmbSelect->SelectedIndex == 0)
{
x = Decimal::ToInt32(updownX->Value);
y = Decimal::ToInt32(updownY->Value);
CNNBot newBot(x,y,pnlGameZone);
CNNBot * temp = &newBot;
VList().Add(temp);
}
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
}
private: System::Void tmrspeed_Tick(System::Object^ sender, System::EventArgs^ e)
{
VList().moveAll();
VList().checkCollisions();
VList().destroy();
}
};
};
This is the error message i get.
Error 1 error LNK2020: unresolved token (06000003) Prog3.Form1::VList C:\Users\Duerst\Documents\Visual Studio 2010\Projects\Prog 3\Prog 3\Prog 3.obj Prog 3
I am a novice programmer so it is probably something pretty basic, but it would be great if you guys could find what I've done wrong.
The problem is that when you declare VList like this:
BotContainer VList();
The compiler thinks it is a function instead of an object because of the () operator.
To fix it, remove the parenthesis ()
BotContainer VList;
And also remove the parenthesis on very ocurrence of VList:
VList.Add(temp);
instead of:
VList().Add(temp);
And:
VList.moveAll();
VList.checkCollisions();
VList.destroy();
instead of:
VList().moveAll();
VList().checkCollisions();
VList().destroy();