So when I try to simply compile my code using "g++ Asg5.cpp" I receive the following error
/tmp/cczhpSGO.o: In function 'main':
Asg5.cpp:(.text+0x2fb): undefined reference to 'BinomialTree::insert(int)'
collect2: ld returned 1 exit status
If anyone's wondering why I'm not using a makefile, my professor simply wants to type g++ <.cpp with main()> to compile..
Anyway here's my code I really appreciate the assistance!
Asg5.cpp
#include "BinomialTree.h"
#include "BinomialNode.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, char* argv[])
{
//input handling
if(argc != 2)
{
cout << "Incorrect Usage. \n Example: ./a.out <filename>" << endl;
exit(1);
}
BinomialTree *tree = new BinomialTree();
char *buffer;
char *token;
//read file into buffer.**************************************
string input;
ifstream file;
file.open(argv[1]);
if(file.is_open())
{
string str;
while(file.good())
{
getline(file,str);
input += " " + str;
}
}
else{
cout << "File not found"<< endl;
return 1;
}
file.close();
int buf;
stringstream ss(input);
vector<int> tokens;
while(ss >> buf)
{
tokens.push_back(buf);
}
int i = 0;
for(i = 0; i < tokens.size(); i++)
tree->insert(tokens[i]);
//end file reading *******************************************
delete tree;
}
BinomialNode.h
#ifndef _BINOMIALNODE_H_
#define _BINOMIALNODE_H_
#include "BinomialTree.h"
class BinomialNode
{
public:
int k;
BinomialNode *children[20];
int data;
BinomialNode();
};
#endif
BinomialNode.cpp
class BinomialNode
{
BinomialNode::BinomialNode(int n)
{
this->k = 0;
this->data = n;
}
}
BinomialTree.h
#ifndef _MULTIMAP_H_
#define _MULTIMAP_H_
#include "BinomialNode.h"
class BinomialTree
{
public:
BinomialNode * BQ[20];
void insert(int n);
void merge(BinomialNode *queue, BinomialNode *in, int k);
void print(BinomialNode *root, int tab);
};
#endif
BinomialTree.cpp
#include "BinomialNode.h"
#include "BinomialTree.h"
#include <iostream>
#include <cstdlib>
class BinomialTree
{
void BinomialTree::insert(int n)
{
BinomialNode *in = new BinomialNode(n);
if(BQ[0] == NULL)
{
BQ[0] = in;
return;
}
else
merge(BQ[0], in, 0);
}
void BinomialTree::merge(BinomialNode *queue, BinomialNode *in, int k)
{
if(queue == NULL)
{
BQ[k] = in;
return;
}
if(n == NULL)
{
BQ[k] = queue;
return;
}
if(queue->data > in->data)
{
merge(in, queue);
return;
}
queue->k++;
BinomialNode* temp[queue->k];
int i;
for(i = 0; i < queue->k-1; i++)
temp[i] = queue->children[i];
temp[queue->k-1] = in;
for(i = 0; i < queue->k; i++)
queue->children[i] = temp[i];
if(BQ[queue->k] == NULL)
{
BQ[queue->k] = queue;
return;
}
else
merge(queue, BQ[queue->k]);
}
void BinomialTree::print(BinomialNode *root, int tab)
{
if(root == NULL)
return;
int i;
for(i = 0; i < tab*5; i++) cout << " ";
cout << root->data << endl;
for(i = 0; i < root->k; i++) print(root->children[i], tab+1);
}
}
You cpp files shouldn't have Class in them. They should look more like:
BinomialNode.cpp
#include "BinomialNode.h"
BinomialNode::BinomialNode(int n) :
k(0)
{
data = n;
}
And of course the corollary for the much longer BinomialTree.cpp. Also, you should compile it with something like:
g++ BinomialTree.cpp BinomialNode.cpp Asg5.cpp -o asg5
Also you're going to run into a lot of other problems with you code. For instance:
BinomialNode * BQ[20];
I don't see BQ being initialized anywhere, which means you're pretty much guaranteed a seg fault if you were to run this. You need to initialize this or allocate it. Seeing lines like:
if(BQ[0] == NULL)
Makes me think you really wanted:
BinomialNode BQ[20];
Though you would still need to initialize it to all NULLs since you aren't guaranteed that will be full of NULLs when you run the program. Also, this is recursive and infinite and can't possibly work (in BinomialNode.h):
BinomialNode *children[20];
There are likely more issues with this code, but that wasn't your question, so I'll stop now!
You need to compile all the .cpp files into one program. You're not using BinomialTree.cpp anywhere right now. If your project requires that you simply type g++ Asg5.cpp then you need to #include your .cpp files or move their contents to your .h files. But the normal solution would be to build all the .cpp files together, not just the one containing main().
Related
Okay so the project is to create a lottery number composed of 10 random positive integers and the user is suppose to guess it until the user guesses the correct number. All of my code looks good but when I run the program and enter in a number it gives me this MSVS Runtime Library error? I dont even know what it means as I am fairly new to programming. Help would be very appreciated!
Main.cpp
#include <iostream>
#include <cmath>
#include <ctime>
#include "Lottery.h"
using namespace std;
int main() {
const int size = 9; //declare variables
int win[size];
int g;
srand(time(NULL));
assign(win, size);
draw(win, size);
g = entry();
if (check(win,size,g) == true) {
cout << "Congradulations! You have won the lottery!" << endl;
}
else {
cout << "Try again!" << endl;
}
printOut(g);
}
Lottery.cpp
#include <iostream>
#include <cmath>
#include "Lottery.h"
using namespace std;
int entry() {
int guess;
cout << "Enter a number from 0 to 99." << endl;
cin >> guess;
return guess;
}
void assign(int w[], int s) {
for (int i = 0; i < s; i++) {
w[s] = -1;
}
}
bool check(int w[], int s, int g) {
for (int i = 0; i < s; i++) {
if (g == w[i]) {
return true;
}
}
return false;
}
void draw(int w[], int s) {
for (int i = 0; i < s; i++) {
int tmp = rand() % 100;
if (check(w, s, tmp)) {
i--;
}
else
w[i] = tmp;
}
}
void printOut(int g) {
cout << "Numbers you have chosen:" << " " << g << endl;
}
Lottery.h
#ifndef LOTTERY_INCLUDED
#define LOTTERY_INCLUDED
void assign(int[], int);
bool check(int[], int, int);
void draw(int[], int);
int entry();
void printOut(int);
#endif //LOTTERY
Debugging tutorials are available elsewhere. But if something bad happens, don't panic and look for instructions.
First, your runtime error:
This has a link "Break and open exception settings" link or a "Break" button. Break which will take you to the end of main if you click it.
The details say we did something bad near win.
Look at this:
void assign(int w[], int s) {
for (int i = 0; i < s; i++) {
w[s] = -1; //<------Oh oops!
}
}
We know the length of the array is s i.e. 9, and are using w[s] where we clearly meant w[i].
The extra details in the error are telling you a possible place to look.
Okay, so my entire program is shown below. For some reason, when the partition function is called, it is throwing a stack overflow error. I have poured over the code and searched for help. You fine programmers are my last hope. Everything else works fine, or at least as well as it needs to. I'd appreciate it if you could look at the Quicksort and the partition functions for me and see if you can figure out where I messed up.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <dos.h>
using namespace std;
vector<int> DataIn(ifstream&);
void quickSort(int, int, vector<int>&, int);
int partition(vector<int>& list, int start, int end)
{
int pivot = list[start];
int index = start;
for (int i = start + 1; i < end; i++)
{
if (list[i] <= pivot)
{
swap(list[index], list[i]);
}
}
index++;
if (index != end)
{
swap(list[index], list[start]);
}
return index;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int repeat = 0;
int fileCount = 1;
while (repeat == 0)
{
int loadFail = NULL;
cout << "\nWhat is the file name: ";
string fileName;
cin >> fileName;
ifstream fileIn(fileName);
do
{
if (fileIn.fail())
{
loadFail = 1;
cout << "\nUnable to open file. Please try again:";
cout << "\nWhat is the file name: ";
cin >> fileName;
ifstream fileIn(fileName);
if (fileIn.good())
{
loadFail = 0;
}
}
else
{
loadFail = 0;
}
} while (loadFail == 1);
vector<int> fileData;
fileData = DataIn(fileIn);
int fileLength = fileData.size();
void quickTime = quickSort(0, fileLength - 1, fileData, fileCount);
return 0;
};
vector<int> DataIn(ifstream& read)
{
vector<int> data;
int dataLine;
while (!read.eof())
{
read >> dataLine;
read.ignore();
data.push_back(dataLine);
}
return data;
}
void quickSort(int begin, int end, vector<int>& list, int fileNum)
{
int mid = 0;
if (end > begin)
{
mid = partition(list, begin, end);
quickSort(begin, mid, list, fileNum);
quickSort(mid + 1, end, list, fileNum);
}
return elapsed_time;
}
You have a few major issues in your posted code.
You are trying to return a value in void function (quickSort). Also I don't see where you declared that variable.
You are declaring a type void variable, which is wrong. A void function return void, that means that it doesn't return anything.
You have a missing bracket at the end of the main() function.
Your while loop will never stop, because repeat is always equal with 0.
In your quick sort function, in the first recursive call there should be mid-1
Your partitioning function logic is wrong
Also filenum variable is not used anywhere.
Here is a modified version of your code, that does work.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <dos.h>
using namespace std;
vector<int> DataIn(ifstream&);
void quickSort(int, int, vector<int>&);
int partition(vector<int>& arr, int left, int right)
{
int pivot = arr[left];
while (left != right)
{
if (arr[left] > arr[right])
{
swap(arr[left], arr[right]);
}
if (pivot == arr[left])
right--;
else
left++;
}
return left;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int repeat = 0;
int fileCount = 1;
int loadFail = NULL;
cout << "\nWhat is the file name: ";
string fileName;
cin >> fileName;
ifstream fileIn(fileName);
do
{
if (fileIn.fail())
{
loadFail = 1;
cout << "\nUnable to open file. Please try again:";
cout << "\nWhat is the file name: ";
cin >> fileName;
ifstream fileIn(fileName);
if (fileIn.good())
{
loadFail = 0;
}
}
else
{
loadFail = 0;
}
} while (loadFail == 1);
vector<int> fileData;
fileData = DataIn(fileIn);
int fileLength = fileData.size();
quickSort(0, fileLength - 1, fileData);
return 0;
}
vector<int> DataIn(ifstream& read)
{
vector<int> data;
int dataLine;
while (!read.eof())
{
read >> dataLine;
read.ignore();
data.push_back(dataLine);
}
return data;
}
void quickSort(int begin, int end, vector<int>& list)
{
int mid = 0;
if (end > begin)
{
mid = partition(list, begin, end);
quickSort(begin, mid-1, list);
quickSort(mid + 1, end, list);
}
}
So, I'm using dev-C++. The compiler works fine, a simple hello world program works along with about a dozen other simple programs. This is a work-in-progress of something I'm working on for class.
For me this compiles but it never runs. What's wrong with it?
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
void getNames(vector<string> &vectorName, int &last, string temp);
int main() {
vector<string> names;
string tmp;
int last = 0;
getNames(names, last, tmp);
for(int j = 0; j < last; j++) {
cout << names.at(j) << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
void getNames(vector<string> vectorName, int &last, string temp) {
while (true) {
cout << "Enter a name (quit to stop): ";
cin >> temp;
if (temp == "quit") break;
vectorName.push_back(temp);
last = vectorName.size();
}
}
The program should fail to link because it can't find the definition for:
void getNames(vector<string> &vectorName, int &last, string temp);
That's because you're missing the & in your definition:
void getNames(vector<string> vectorName, int &last, string temp){
^^^^^^^^^^^
Add in the & and it should compile and run just fine.
First your getNames declaration and implementation signatures are not exactly the same.
void getNames(vector<string> &vectorName, int &last, string temp){
void getNames(vector<string> vectorName, int &last, string temp){
So I'm trying to create a 2 dimensional array of pointers to a object of type Piece. The problem is when i try assign a pointer to a piece to the array i get a segmentation fault. I realized I needed to initialize to array to sometime before I can start allocating but I can't get it right.
Here is the header file of Map which contains a 2-d array of pointers.
#ifndef MAP_H
#define MAP_H
#include <iostream>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <cstring>
#include "Player.h"
#include "Sprite.h"
#include "Piece.h"
#include "Messages.h"
#include "PieceType.h"
using namespace std;
class Map
{
private:
Piece*** pieces;
int startingX;
int startingY;
int width;
int height;
string mapName;
public:
Map(string);
~Map();
void printMap() const;
Piece* pieceType(char);
void setSprite(Piece*);
void firstMove();
void resetMap(string);
bool moveUp(int, int);
bool moveDown(int, int);
bool moveLeft(int, int);
bool moveRight(int, int);
int getHeight();
int getWidth();
};
#endif
The array I'm talking about is pieces.
I try to allocate this in the constructor of Map.
Map::Map(string name)
{
ifstream map;
string line;
string dimention;
mapName = name;
map.open(name.c_str());
if (map.good())
{
getline (map, line);
int i = 0;
while(line[i] != 'X')
{
dimention[i] = line[i];
i++;
}
stringstream convert(dimention);
convert >> width;
int temp = i;
dimention = "";
i = 1;
while(line[(i + temp)] != '\0')
{
dimention[i] = line[(i + temp)];
i++;
}
stringstream convertTwo(dimention);
convertTwo >> height;
for (int i = 0; i < height; i++)
{
if (!(map.eof()))
{
getline (map, line);
}
else
{
cout << "Error with file" << endl;
break;
}
for (int j = 0; j < width; j++)
{
pieces[i][j] = pieceType(line[j]); //This is where I'm getting the segmentation fault
cout << "assigned" << endl;
if ((pieces[i][j])->getType() == WAYPOINT)
{
if (pieces[i][j]->getWaypointType() == 0)
{
startingX = j;
startingY = i;
}
}
else
{
(pieces[i][j])->setXCordinate(j);
(pieces[i][j])->setYCordinate(i);
}
}
}
}
}
Where name is a string that holds the name of the file that has the information for loading a particular map.
Also the function pieceType is as follows:
Piece* Map::pieceType(char type)
{
Piece* temp;
if (type == '.')
{
return NULL;
}
if (type == 'S')
{
temp = new Waypoint(0);
return temp;
}
if (type == 'E')
{
temp = new Waypoint(1);
return temp;
}
}
Waypoint is a derived class of Piece.
The problem is indeed that you have to initialize that array. Like this:
pieces=new Piece**[height];
for(int i=0;i<height;i++){
pieces[i]=new Piece*[width];
}
Write that just after you get width and height, and before you start using pieces.
But something you should know: for each new, there should be a corresponding delete, or else that memory will never be freed, and you will get a memory leak. To free that memory, add this in your destructor:
for(int i=0;i<height;i++){
for (int j = 0; j < width; j++){
delete pieces[i][j];
}
delete[] pieces[i];
}
delete[] pieces;
This assumes that every pieces[i][j] contains either an object allocated with new or NULL, and it works with both. Looking at your code, that seems to be your case. However, it would not work if one of them is not assigned (not your case).
Use std::vector<std::vector<Pieces>>instead of (trying to, because it does not work) reinventing the wheel. Its safe, easy, and avoids getting manual-memory-management headaches.
I'm having trouble working with strings (char*) in structs. I can't seem to call the right data I want too.
Under processFile it shows the struct member correctly; under main it does not.
Here is my code:
#include <iostream>
#include <io.h>
#include <string>
#include "dirent.h"
#include "Stream.h"
#include "Compression.h"
#include "Definitions.h"
using namespace std;
using namespace System::IO;
using namespace System::Funcs;
bool isRawfile(char* ext);
void ProcessDirectory(string directory);
void ProcessFile(char* file);
void ProcessEntity(struct dirent* entity);
typedef struct
{
char *name;
int usize;
int csize;
BYTE *data;
} rawfile;
string path = "";
string source;
int numrawfiles = 0, numstringtables = 0;
rawfile *rawfiles = new rawfile[0x400];
FILE * zone = fopen( "C:\\Users\\jake\\Desktop\\patch_mp.zone" , "wb" );
int main(int argc, char **args)
{
if(args[1] != NULL)
{
source = string(args[1]) + "\\"; //maybe move under else here..
if(strchr(args[1], '.') != NULL)
{
cout<<"Unable to compile files, please drag a folder to compile."<<endl;
cin.get();
return 0;
}
else
{
int header[] = {1,0,0x3B4,0,0,0,1,0,0x1000,0,0,0,-1};
for(int i=0; i<13; i++)
fwrite(Converter::Int32ToBytes(header[i]), 1 , 4 , zone );
ProcessDirectory(args[1]);
for(int i=0; i<numrawfiles; i++)
cout<<"Name: "<<rawfiles[i].name<<" Length: "<< rawfiles[i].usize << " - in main()"<<endl;
fclose(zone);
}
}
else
{
cout<<"No folder selected to compile. Press any Key to quit."<<endl;
cin.get();
return 0;
}
cin.get();
return 0;
}
void ProcessDirectory(string directory)
{
string dirToOpen = path + directory;
auto dir = opendir(dirToOpen.c_str());
path = dirToOpen + "\\";
if(NULL == dir)
{
cout << "could not open directory: " << dirToOpen.c_str() << endl;
return;
}
auto entity = readdir(dir);
while(entity != NULL)
{
ProcessEntity(entity);
entity = readdir(dir);
}
path.resize(path.length() - 1 - directory.length());
closedir(dir);
}
void ProcessEntity(struct dirent* entity)
{
if(entity->d_type == DT_DIR)
{
if(entity->d_name[0] == '.')
return;
ProcessDirectory(string(entity->d_name));
return;
}
if(entity->d_type == DT_REG)
{
string fullpath = path + entity->d_name;
ProcessFile(const_cast<char *>(fullpath.c_str()));
return;
}
cout << "Not a file or directory: " << entity->d_name << endl;
}
void ProcessFile(char* file)
{
char* extension = strrchr(file, '.');
if(isRawfile(extension))
{
rawfile raw;
raw.name = (char *)&file[source.length()];
raw.usize = File::getFileSize(file);
rawfiles[numrawfiles] = raw;
cout<<"Name: "<<rawfiles[numrawfiles].name<<" Length: "<< raw.usize << " - in ProcessFile()"<<endl;
fwrite(Converter::Int32ToBytes(0x23),1,4,zone);
fwrite(Converter::Int32ToBytes(-1),1,4,zone);
numrawfiles++;
}
}
bool isRawfile(char* ext)
{
char *exts[11] = {".gsc",".cfg",".txt",".news",".png",".vision",".rmb",".script",".arena",".atr",".csc"};
for(int i=0; i<10; i++)
if(strncmp(ext,exts[i],strlen(exts[i]))==0)
return true;
return false;
}
Here is an example picture:
What am I doing wrong?
Save yourself a lot of trouble by using an std::string:
#include <string>
struct thing
{
std::string name;
int age;
};
You can also avoid the dynamically allocated array:
#include <vector>
std::vector<thing> things(3);
You are outputting the memory address of "Ben" instead of the actual String. You should use
cout << things[1]->name << endl;
which is syntactic sugar for
cout << (*things[1]).name << endl;
To add to what scd said, you are not successfully dereferencing the name member. When you try and operate on things[1].name you are operating on a pointer, which is simply a memory location.
This is one of the trickiest things when learning to use pointers. Here is some more reading on the dereference operator, and syntax hints.
http://en.wikipedia.org/wiki/Dereference_operator#Other_syntax
Edit:
After compiling myself, I realized that I was on the wrong track with this one, and that std::cout will correctly handle the char pointer. You should be able to solve this with your code, just ensure that you give your struct array a size.
This worked for me:
#include <iostream>
#define MAXSIZE 3
typedef struct
{
char* name;
int age;
}Thing;
Thing *things = new Thing[MAXSIZE];
int _tmain(int argc, _TCHAR* argv[])
{
char* names[MAXSIZE] = { "Alice", "Ben", "Carlos" };
int ages[MAXSIZE] = { 24, 25, 26 };
for(int i=0; i<MAXSIZE; i++)
{
things[i].name = names[i];
things[i].age = ages[i];
}
std::cout << things[1].name << std::endl;
return 0;
}