I have this code
main.cpp
#include <iostream>
#include "functs.h"
using namespace std;
int main()
{
ArrayList *al = new ArrayList;
return 0;
}
functs.h
using namespace std;
#ifndef FUNCTS_H_INCLUDED
#define FUNCTS_H_INCLUDED
class ArrayList;
#endif // FUNCTS_H_INCLUDED
functs.cpp
#include <iostream>
#include "functs.h"
class ArrayList{
public:
void add(int num);
void add(int num, int index);
void remove(int index);
void removeNum(int num);
string toString();
ArrayList(int init);
private:
void resize();
int size, cap;
int *myList[10];
};
void ArrayList::add(int num){
if (size>=cap/2)
{
resize();
}
*myList[size] = num;
size++;
}
void ArrayList::resize(){
int temp[cap*2];
int i;
for (i = 0; i < size; i++)
{
temp[i] = *myList[i];
}
*myList = temp;
}
ArrayList::ArrayList(){
size = 0;
cap = 10;
}
void ArrayList::add(int num, int index){
int temp = *myList[index];
int i;
for (i = index; i < size; i++)
{
*myList[index] = num;
num = temp;
temp = *myList[i+1];
}
add(temp);
}
string ArrayList::toString(){
string returnString = "{";
int i;
for (i = 0; i < size; i++)
returnString.append(*myList[i] +",");
returnString.replace(returnString.length()-1,1,"}");
return returnString;
}
and I'm extremely new to C++ but whenever I try to compile the code it gives me a "size of ArrayList is not know". Please help me figure out the error. =(
Design/usage problems in your code notwithstanding, the most obvious problem is that you want to put the class definition in the functs.h file instead of the functs.cpp file:
functs.h:
// This is declaration is highly not recommended for use in header files.
// using namespace std;
#ifndef FUNCTS_H_INCLUDED
#define FUNCTS_H_INCLUDED
#include <string>
class ArrayList{
public:
void add(int num);
void add(int num, int index);
void remove(int index);
void removeNum(int num);
std::string toString();
ArrayList(int init);
private:
void resize();
int size, cap;
int *myList[10];
};
#endif // FUNCTS_H_INCLUDED
functs.cpp:
#include "functs.h"
void ArrayList::add(int num){
if (size>=cap/2)
{
resize();
}
*myList[size] = num;
size++;
}
void ArrayList::resize(){
int temp[cap*2];
int i;
for (i = 0; i < size; i++)
{
temp[i] = *myList[i];
}
*myList = temp;
}
ArrayList::ArrayList(){
size = 0;
cap = 10;
}
void ArrayList::add(int num, int index){
int temp = *myList[index];
int i;
for (i = index; i < size; i++)
{
*myList[index] = num;
num = temp;
temp = *myList[i+1];
}
add(temp);
}
std::string ArrayList::toString(){
std::string returnString = "{";
int i;
for (i = 0; i < size; i++)
returnString.append(*myList[i] +",");
returnString.replace(returnString.length()-1,1,"}");
return returnString;
}
templatetypedef provides a reason why this is necessary. Basically the compiler needs to know how much space an ArrayList needs, and a class ArrayList; provides no such information.
It's not a good idea to declare using namespace std; inside a header file, because then everyone that includes the functs.h file (including your clients!) will also have a using namespace std;, increasing the possibility of name collisions.
I highly recommend that you pick up a good introductory C++ book if you wish to learn C++ properly. You demonstrate in your question a rather big misunderstanding of how good C++ is written. That's not to say you're incompetent as a person, but there are some serious problems with the code you present. Just to name a few:
Standard C++ already provides a perfectly fine array class called std::vector. There's no need to reinvent the wheel for what you're doing. And even if you have to reinvent the wheel, an advanced understanding of C++ and plenty of C++ experience is a prerequisite to implementing an array container that's appropriate for production use.
The public interface of your class is incomplete. There's no way for clients to know how many elements are actually in the array.
int *myList[10]; declares a fixed array of 10 pointers to an int. This is not appropriate for an array class. Especially if you want the array to be resizable.
There's not sufficient memory management for this class to be useful in any sense. There are no destructors and apparently the constructors are not complete (nor do they match), so you have no real logical place to put things like new[] and delete[].
You have a ArrayList *al = new ArrayList; but you don't have a corresponding delete al; anywhere. This is a memory leak.
But the last point is moot because you should be using ArrayList a1; instead of ArrayList *al = new ArrayList;. The former will automatically "delete" itself at the end of the scope (in this case, the main() function) while the latter requires a delete statement. C++ is not like Java where unused new'ed objects are automatically collected.
I can't comment on the correctness of the algorithms you used, because (and I'm sorry to say this because it'll sound harsh) what you have simply won't work. Again, I recommend that you pick up a good introductory C++ book, which will cover these kinds of issues. (I must emphasize that none of these shortcomings are a statement of you as a person. I'm talking specifically about the code you have in your question.)
The reason that you're getting this error is that in main.cpp, the compiler hasn't seen the definition for the class ArrayList. It's only seen the declaration
class ArrayList;
When you try to create the ArrayList by writing
new ArrayList;
The compiler doesn't know how much memory is needed to hold an ArrayList because it hasn't seen the class definition. This contrasts with other languages like Java, where this information doesn't have to immediately be available.
To fix this, update your .h file by moving the definition of the class from the .cpp file. That way, when someone #includes the header file, they'll get the class definition in addition to the declaration, which will allow you to use new, declare local variables of type ArrayList, etc.
Hope this helps!
This much
class ArrayList{
public:
void add(int num);
void add(int num, int index);
void remove(int index);
void removeNum(int num);
string toString();
ArrayList(int init);
private:
void resize();
int size, cap;
int *myList[10];
};
Should be in the .h file.
Why? Because the declaration of a class (when you write class ArrayList;) is only enough when the size of the class is not needed (more specific cases are listed in the C++ standard). The definition of the class should appear in the same translation unit in which the class is used in a way in which it is required to be complete.
The way you've declared myList it has a fixed size; *myList = temp; isn't doing what you want it to.
Declare myList simply as int *myList;
In the constructor, use myList = new int[10];
Everywhere you have *myList[...] change it to myList[...]
In resize, int temp[cap*2] needs to be int *temp = new int[cap * 2] and *myList = temp needs to be myList = temp
You'll still have a memory leak, but that should get you started.
Related
I'm making a heap class to be importable with heap.h and my constructors including bool types do not work, yet every other constructor and function imported works.
Here is what's in heap.h:
#ifndef __HEAP_INCLUDED__
#define __HEAP_INCLUDED__
#include <iostream>
#include <vector>
using namespace std;
class heap{
int capacity;
bool isMinHeap; //1 is min heap -- ascending order
vector<int> * content;
public:
heap();
heap(bool t);
heap(vector<int> * input);
heap(vector<int> * input, bool t);
void print();
void prettyPrint();
int parent(int i);
int leftChild(int i);
int rightChild(int i);
int size();
int getMax();
void insert(int data);
void heapifyDown(int index);
void heapifyUp(int index);
int invalidChild(int index);
int deleteMax();
int deleteMin();
bool minDir();
int at(int index);
};
vector<int> * heapSort(vector<int> * input);
void swap(vector<int> * vec, int a, int b);
#endif
Here are the defined constructors in heap.cpp. Note, all constructors work fine when I add a main to this file to test stuff:
class heap{
vector<int> * content;
int capacity = 256;
bool isMinHeap; //1 is min heap -- ascending order
public:
heap(){
content = new vector<int>;
isMinHeap = 0;
}
heap(bool t){
content = new vector<int>;
isMinHeap = t;
}
heap(vector<int> * input){
content = input;
isMinHeap = true;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
heap(vector<int> * input, bool t){
content = input;
isMinHeap = t;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
//other functions below
}
The constructors with bool do not work in main.cpp, which has #include "heap.h" at the top. The files are all in the same directory and I am compiling with this command: g++ heap.cpp main.cpp -o main. Why do two of my constructors not work?
The error I see is
/usr/bin/ld: /tmp/ccwomODk.o: in function `main':
main.cpp:(.text+0x4e2): undefined reference to `heap::heap(bool)'
collect2: error: ld returned 1 exit status
-Wall does not elaborate on the issue. I'm pretty sure the issue is with my linking somewhere because the constructors work inside of heap.cpp when I use them in there.
What you are doing with the class in the .cpp file is wrong. You are not allowed to define the class twice. There must only be one class heap { /*...*/ }; in the program (but it may be included in multiple .cpp files). Otherwise the one-definition-rule (ODR) is violated and the program has undefined behavior.
So remove everything you are showing from heap.cpp.
To define the constructors of heap in the heap.cpp file, you need to use this syntax:
#include "heap.h"
heap::heap() {
/*...*/
}
heap::heap(bool t) {
/*...*/
}
//...
and so on. The other member functions must be defined in a similar way, e.g.:
void heap::print() {
/*...*/
}
Furthermore, if you want to have a default member initializer as in
int capacity = 256;
add it in the declaration in the .h file instead.
I also want to add that having a pointer-to-std::vector as member is almost surely a wrong approach as well, but out-of-scope for the question.
When you declare a program element such as a class, function, or
variable, its name can only be "seen" and used in certain parts of
your program. The context in which a name is visible is called its
scope. For example, if you declare a variable x within a function, x
is only visible within that function body.
It seems you broke ODR rule so bad. Your class members including constructors has no body declared in the source file(heap.cpp).
Use '::' to make class members have a body:
//heap.cpp
"heap.h"
heap::heap()
{
}
heap:heap(vector<int> * input, bool t)
{
}
int heap::parent(int i)
{
return i;
}
// this is how you create a body for function that are class members
// the same should be done for all other functions
I am trying to create an array in my UnsortedList class. I specified to create an array in the header file, and I also specified the MAX_SIZE, which is equal to 10. However, whenever I create my object of the class, the default constructor does not create that array with the MAX_SIZE. I am unsure what I am doing wrong. I also get an error saying "stack around the variable 'myList' was corrupted". Also, just as a side note, can I initialize the array values when the default constructor is called, instead of creating a function to do it?
"UnsortedList.h" header file:
#pragma once
class UnsortedList {
public:
UnsortedList();
bool IsFull(); //Determines whether the list is full or not (returns T or F)
int GetLength(); //Gets the length of the list
void SetListValues();
private:
int length;
const int MAX_ITEMS = 10;
int numbers[];
};
"UnsortedList.cpp" file:
#pragma once
#include "UnsortedList.h"
#include <fstream>
#include <iostream>
using namespace std;
UnsortedList::UnsortedList() {
length = 0; //sets length to 0
numbers[MAX_ITEMS]; //sets array maximum size to MAX_ITEMS (10 as indicated in UnsortedList.h)
}
bool UnsortedList::IsFull() {
return (length == MAX_ITEMS);
}
int UnsortedList::GetLength() {
return length;
}
void UnsortedList::SetListValues() {
ifstream inFile;
inFile.open("values.txt");
int x = 0;
while (!inFile.eof()) {
inFile >> numbers[x];
x++;
}
}
"main.cpp" file:
#include <iostream>
#include <string>
#include "UnsortedList.h"
using namespace std;
int main() {
UnsortedList myList;
myList.SetListValues();
return 0;
}
I recommend you use std::array or std::vector, but if you must use C arrays, then your definition in the header needs correcting:
class UnsortedList {
// ...
const static int MAX_ITEMS = 10;
int numbers[MAX_ITEMS];
};
You can remove the corresponding line in the constructor. The file reading method also needs correcting:
void UnsortedList::SetListValues() {
ifstream inFile;
inFile.open("values.txt");
int x = 0;
int read_value;
// x < MAX_ITEMS to avoid out of bounds access
while (x != MAX_ITEMS && inFile >> read_value)
{
numbers[x++] = read_value;
length++; // I assume you also want to increment the length at this point?
}
}
Edit: As noted by #πάνταῥεῖ, there is no good reason to use C style arrays when the standard provides std::array. Not much changes, it is declared as:
std::array<int, MAX_ITEMS> numbers;
You can use operator[] as with the C array. This is preferable as it provides a richer API and can be used like other C++ containers, i.e. with STL algorithms.
I have 2 classes.
First Class - Midgam - The constructor has the following line:
midgam = new Vector[20];
The second class - Vector - where I create an array named array.
The program works great just that I have a little problem.
At the end of the program I try to print in alphabetical order, I use the BubbleSort sorting. The sorting works fine but something in the Swap function stops.
This is how it looks:
void Midgam::Swap(Vector *xp, Vector *yp) {
Vector temp = *xp;
cout << temp.getName() << endl;
*xp = *yp;
*yp = temp;
}
void Midgam::bubbleSort() {
int i, j;
for (i = 0; i < iterator - 1; i++) {
for (j = 0; j < iterator - i - 1; j++) {
if (midgam[j].getName().compare(midgam[j+1].getName()) > 0) {
Swap(&midgam[j], &midgam[j+1]);
}
}
}
}
I work with Visual Studio, the program stops and the program shows me the following code snippet in the Vector class:
Vector::~Vector() {
if (array)
delete[] array;
}
full definitions of Midgam:
#include <iostream>
#include <string>
#include "Vector.h"
using namespace std;
#ifndef MIDGAM_H_
#define MIDGAM_H_
class Midgam {
private:
int boxNum;
int maxParties;
int iterator;
Vector *midgam;
public:
Midgam(int num_of_boxes, int num_of_parties);
virtual ~Midgam();
void Start();
void Menurmal();
void SumOfEzor();
double SumOfParty(string name);
int SumAllVotes();
void AddParty();
void Swap(Vector *xp, Vector *yp);
void bubbleSort();
void Histograma();
void PrintStars(int num);
int FindPartyByName(string party);
void PrintAll();
};
#endif /* MIDGAM_H_ */
full definitions of Vector:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#ifndef VECTOR_H_
#define VECTOR_H_
class Vector {
private:
string name;
int size;
unsigned int *array;
bool Bool;
public:
Vector(string name, int size);
Vector();
Vector & operator=(const Vector &);
virtual ~Vector();
bool StringToArray(string str);
bool getBool();
string getName();
unsigned int getAddress();
int getSize();
unsigned int getValueFromArray(int index);
double sumOfArray();
void PrintArray();
};
#endif /* VECTOR_H_ */
Does anyone know why it does not work? Thank you
Your Vector lacks a proper copy constructor.
Vector temp = *xp;
//NOT EQUAL TO:
//Vector temp;
//temp=*xp;
The above statement won't call operator=(const Vector &) even though there's an equal sign. The following line is correct and equivalent:
Vector temp(*xp);
The reason is that this is a copy initialization - temp is created and so a constructor must be called - in particular the copy constructor Vector(const Vector &). Which you did not explicitly declared and so a default one was used.
Then a shallow copy is made, temp and *xp then share the same array and when both their destructors get called the second one will try to delete already deleted memory - undefined behavior which triggers Visual Studio's debugger (at least in debug mode).
The solution is to do a proper deep copy - create a new array and copy its contents:
#include <algorithm> #Contains std::copy_n
Vector::Vector(const Vector& other)
{
name=other.name;
size=other.size;
//Creates a new array
array= new unsigned int[size];
//Copies the array contents
std::copy_n(other.array,size,array);
Boo=other.Bool;
}
Also this is a prime example of why not to use raw memory. I get that you are implementing custom vector and don't want to use std::vector for the array but at least use std::unique_ptr. If you would have just done that you wouldn't have to ask this question in the first place as the compiler would have complained and the debugger wouldn't have to do the compiler's job.
I have to make a class that will make arrays act like vectors. When I try and pass the class into the method into my main I get an error telling me that "[" and "]" are incorrect operators. I was wondering if I'm just completely doing this wrong or if it's just a simple mistake. Help is greatly appreciated. Here is my header file:
#ifndef PROGRAM5HEADER_H
#ifndef PROGRAM5HEADER_H
#define PROGRAM5HEADER_H
#include <string>
using namespace std;
class FloatArray
{
int *rep;
int _size;
public:
FloatArray(int sz=100):_size(sz)
{
rep=new int[sz];
}
~FloatArray()
{
delete [] rep;
}
int size() const
{
return _size;
}
FloatArray(const FloatArray& x)
{
copy(x);
}
void copy(const FloatArray& x)
{
_size == x.size();
rep=new int[_size];
for(int k=0;k<_size;k++)
rep[k]=x.rep[k];
}
};
#endif
and here is my main program
#include <iostream>
#include <string>
#include <cstdlib>
#include "program5header.h"
#include <cmath>
using namespace std;
int meanstd(FloatArray x, int& std)
{
int sx=0,sx2=0,mean;
for(int i=0;i<x.size();i++)
{
sx+=x[i];
sx2+=x[i]*x[i];
}
mean=sx/x.size();
std=sqrt(sx2/x.size()-mean*mean);
return mean;
}
int main()
{ int f;
cout<<"How big of an array would you like: "<<endl;
cin>>f;
FloatArray x(f);
}
There are a lot of issues with a lot of your implementation, I'd suggest doing some research on the subject. I'll touch on a few.
Firstly, you should make your FloatArray a templated class and allow for different types other than just int.
When you initialize a FloatArray x and then try to access it's underlying array through "[]" you are actually invoking the following:
x.operator[](...)
You haven't defined the '[]' operator on your FloatArray class so you are getting an error.
You need something similar to this:
int FloatArray.operator[](int index) {
assert(index < _size);
return _rep[index]
}
Your copy isn't doing what you want, it's not copying the size over to "this". It should look something similar to this:
void copy(const FloatArray& x)
{
_size = x._size;
rep=new int[_size];
for(int k=0;k<_size;k++)
rep[k]=x.rep[k];
}
However I would suggest not having a copy method and instead implement everything in your copy constructor.
I'm currently writing a container in C++, but I encounter problem with memory leak in it.
For test purpose I created 2 versions of main:
First one created and delete object from list in one loop(everything OK here)
Second one create object in first loop and delete all of them in second one(after end of both loops it's still a lot of memory used by program).
OS: Windows Seven;
IDE: CodeBlock 12.11;
Compiler: GNU GCC
Code
(placed in one code block, sorry if it's unclear for someone, just want to make this post as short as possible)
main.cpp:
#include <iostream>
using namespace std;
#include "planetarySystemContainer.hpp"
int main()
{
PlanetarySystemContainer * container;
container=new PlanetarySystemContainer();
PlanetarySystem *planet;
int i;
cin>>i;
i=0;
for(int i=0; i<10000000; i++)
{
planet=new PlanetarySystem();
planet->name("blabgs4tegser4labnab");
container->AddAtBegining(planet);
}
for(int i=0; i<10000000; i++)
{
container->DeleteFromBegining();
}
cin>>i;
return 0;
}
planetarySystemContainer.hpp:
class PlanetarySystemContainer
{
private:
PlanetarySystem *First;
PlanetarySystem *Last;
int PlanetarySystemQuantity;
public:
PlanetarySystemContainer();
void AddAtBegining(PlanetarySystem *a);
void DeleteFromBegining();
};
planetarySystemContainer.cpp
#include"planetarySystemContainer.hpp"
PlanetarySystemContainer::PlanetarySystemContainer()
:First(NULL)
,Last(NULL)
,PlanetarySystemQuantity(0)
{
}
void PlanetarySystemContainer::AddAtBegining(PlanetarySystem *a)
{
a->SetPrevious(NULL);
a->SetNext(First);
First=a;
if(a->GetNext()==NULL)
Last=a;
PlanetarySystemQuantity++;
}
void PlanetarySystemContainer::DeleteFromBegining()
{
if(First!=NULL )
{
if(First->GetNext()!=NULL)
{
PlanetarySystem* x=First;
First=First->GetNext();
First->SetPrevious(NULL);
delete x;
}
else
{
PlanetarySystem* x=First;
First=NULL;
Last=NULL;
delete x;
}
PlanetarySystemQuantity--;
}
}
planetarySystem.hpp:
#include <iostream>
class PlanetarySystem
{
private:
PlanetarySystem* Next;
PlanetarySystem* Previous;
unsigned int PlanetQuantity; //ilosc obiektow w ukladzie (planet+stacji+asteroid)
public:
PlanetarySystem();
string name;
PlanetarySystem *GetNext();
PlanetarySystem *GetPrevious();
void SetNext(PlanetarySystem *ps);
void SetPrevious(PlanetarySystem *ps);
int GetPlanetQuantity();
};
planetarySystem.cpp:
#include "planetarySystem.hpp"
PlanetarySystem::PlanetarySystem()
:Next(NULL)
,Previous(NULL)
,PlanetQuantity(0)
{
}
PlanetarySystem *PlanetarySystem::GetNext()
{
return Next;
}
PlanetarySystem *PlanetarySystem::GetPrevious()
{
return Previous;
}
void PlanetarySystem::SetNext(PlanetarySystem *ps)
{
Next=ps;
}
void PlanetarySystem::SetPrevious(PlanetarySystem *ps)
{
Previous=ps;
}
int PlanetarySystem::GetPlanetQuantity()
{
return PlanetQuantity;
};
I see no leak on the first look. But you can easily give a planet a unique name based on its insertion loop number and print the name in the destructor.
This way you can test if all Planets get deleted.
I just ran your code on my Linux box with valgrind after fixing some compile-time errors and reducing the loop count to something reasonable. The only leak valgrind detected is the leaked PlanetarySystemContainer. You aren't deleting it before the program exits.
Regardless, and as I mentioned in my comment, why are you reinventing the wheel by creating a custom container? It seems to me the standard containers will meet your needs just as well. Lastly leverage things like std::unique_ptr<> to automate memory management, or simply store PlanetarySystem objects within the container rather than pointers to them to obviate the need to manage memory.