void set and gets mixed up
Mar 12, 2011 at 3:59am UTC
Update on the coding...Getting errors that I am stumped about. I have been working on this for a few hours not and I am starting to ware down. Line 12 in the first code is giving me the error: "C:UsersCRBottiniDesktopLab3CBArray.h|14|error: return type specification for constructor invalid|
||=== Build finished: 1 errors, 0 warnings ===|
And Lines 109 to 119 in the second coding are causing a crash in the program
We are inputting a file named lab3input.txt into the program and parsing the data. It is a project for school. Any info would help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#pragma once
#include <string>
#include <iostream>
using namespace std;
class CBArray
{
public :
/*********************************************
* Overloaded Constructor for private variables
**********************************************/
void CBArray(int newCapacity, bool newResize, int * newNumbers, int newNumberSize);
/********************************************
* Set Function Declarations for private variables
*********************************************/
void setCapacity(int newCapacity);
void setResize(bool newResize);
void setNumbers(int * newNumbers);
void setNumberSize(int newNumberSize);
/***********************************************
* Get Function Declarations for private variables
************************************************/
int getCapacity();
bool getResize();
int getNumbers();
int getNumberSize();
/************************************************
* Function Declaration for array parsing function
*************************************************/
void processes(string, int );
private :
/**********************
* Variable Declarations
***********************/
int capacity;
bool resize;
int * numbers;
int numberSize;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
#include "CBArray.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void CRArray::setCapacity(int newCapacity)
{
capacity = newCapacity;
}
void CBArray::setResize(bool newResize)
{
resize = newResize;
}
void CBArray::setNumbers(int newNumbers)
{
numbers = newNumbers;
}
void CBArray::setNumberSize(ine newNumberSize)
{
numberSize = newNumberSize;
}
int getCapacity()
{
return capacity;
}
bool getResize()
{
return resize;
}
int getNumbers()
{
return numbers;
}
int getNumberSize()
{
return numberSize;
}
void processes(string nextLine, int count)
{
int capacity = 5;
bool resize = true ;
int * numbers;
int numberSize = 0;
if (count == 1)
{
stringstream ss(nextLine);
ss>>capacity;
cout << capacity << endl;
}
else if (count == 2)
{
stringstream ss(nextLine);
ss>>resize;
numbers = new int [capacity];
}
else if (nextLine[0] == 'p' )
cout << numberSize << '/' << capacity << endl;
else if (nextLine[0] == 'n' )
cout << "array has " << numberSize << " elements" << endl;
else if (nextLine[0] == 'c' )
cout << "array capacity is " << capacity << " elements" << endl;
else if (nextLine[0] == 'a' )
{
if (numberSize<capacity)
{
stringstream ss(nextLine.substr(2));
int value = 0;
ss>>value;
numbers[numberSize] = value;
numberSize++;
cout << value << " added" << endl;
}
else
cout << "array resized to new capacity " << capacity << endl;
}
else if (nextLine[0] == 'i' )
{
stringstream ss(nextLine.substr(2));
int value = 0;
int index = 0;
ss>>value;
ss>>index;
numbers[index] = value;
cout << value << " inserted at index " << index << endl;
}
else if (nextLine[0] == 'g' )
{
stringstream ss(nextLine.substr(2));
int index = 0;
ss>>index;
cout << "value at " << index << ": " << numbers[index] << endl;
}
/*
else if(nextLine[0] == 'r')
{
stringstream ss(nextLine.substr(2));
int index = 2;
ss>>index;
for (int i=index+1; i<count; i++)
numbers[i-1] = numbers[i];
count--;
}
*/
else if (nextLine[0] == '-' )
{
cout << "-1" << endl;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "CBArray.h"
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char ** argv)
{
void processes(string, int );
string filename = "lab3input.txt" ;
if (argc == 2)
filename = argv[1];
cout << filename << endl;
ifstream fin(filename.c_str());
if (!fin.fail())
{
int count = 1;
string nextLine = "" ;
char nextChar = fin.get();
while (nextChar!= EOF)
{
if (nextChar == 'n' )
{
processes(nextLine, count);
nextLine = "" ;
count ++;
}
else
{
nextLine+=nextChar;
}
nextChar = fin.get();
}
}
return 0;
}
Last edited on Mar 12, 2011 at 4:20am UTC
Mar 12, 2011 at 4:01am UTC
Your get functions should return the type the are getting, and need to be declared as methods in the class (you need parentheses), for starters.
Mar 12, 2011 at 4:22am UTC
cant believe I missed that one before. Fixed that but my default constructor is causing errors, and a small chunk commented out in the second code is causing a crash
Mar 12, 2011 at 4:42am UTC
Your constructor does not seem to have an implementation and it shouldn't have a return value.
And as for the crash, you are probably going out of bounds in your array. That's the only thing I can think of without any more details.
Topic archived. No new replies allowed.