I'm working on an x/y coordinate maze solving project.
I have a problem with my Maze object, whose data members are locations which also are objects of int row, int col and enum Direction {right down left up done}.
When I run my debugger, error messages come up that say "invalid use of this outside of non static member function" on the Maze object.
Why is this coming up? I don't understand how this message is showing if I'm not using any this->endLocation statement anywhere in my Maze.cpp file.
#include "maze-proj1.h"
using namespace std;
Maze::Maze() {
validLocationCount = 0;
validLocations = NULL;
startLocation = endLocation;
}
Maze::~Maze() {
if(validLocations != NULL) {
delete [] validLocations;
}
validLocations = NULL;
validLocationCount = 0;
}
Location Maze::getStartLocation() const {
return startLocation;
}
bool Maze::isValidLocation(const Location &loc) const {
int count = 0;
for (int i = 0; i < validLocationCount; i++) {
if (loc == validLocations[i]) {
count++;
}
}
if (count == validLocationCount){
return true;
}
return false;
}
bool Maze::isEndLocation(const Location &loc) const {
return loc == endLocation;
}
istream &operator>>(istream &is, Maze &m) {
is >> m.validLocationCount;
m.validLocations = new Location[m.validLocationCount];
for(int i = 0; i < m.validLocationCount; i++){
is >> m.validLocations[i];
}
is >> m.startLocation;
is >> m.endLocation;
}
class Maze {
public:
Maze(void);
~Maze();
Location getStartLocation(void) const;
bool isValidLocation(const Location &loc) const;
bool isEndLocation(const Location &loc) const;
friend istream &operator>>(istream &is, Maze &m);
private:
Maze(const Maze &) { assert(false); }
const Maze &operator=(const Maze &)
{ assert(false); return *this; }
int validLocationCount;
Location *validLocations;
Location startLocation, endLocation;
};
#endif
Related
I have to finish a prompt tonight for my coding class, and I believe I am writing this incorrectly. I have reviewed other stackoverflow answers, but I still lack the understanding necessary to correctly solve this. This is part of a bigger project, so I have included all code in case another function is causing this error, but in my testing only the Ballot class is giving me errors.
In getVote, if the parameter is within the used portion of the array,
return the Voter pointer that corresponds with that position.
This is supposed to be tested with:
Ballot ballot1("WI-643UWO");
const Vote *vote3 = ballot1.getVote(2);
cout << vote3 << "\n";
//header file
pragma once
#include <string>
using namespace std;
class Election {
private:
string office;
string firstCanidiateName;
string secondCanidiateName;
public:
Election(string office2, string firstCanidiateName2, string secondCanidiateName2);
string getOffice() const;
string getCandidate1() const;
string getCandidate2() const;
};
class Vote {
private:
string vOffice;
string canidiateName;
bool voteMadeInPerson;
public:
Vote(string vOffice2, string canidiateName2, bool voteMadeInPerson2);
string getOffice() const;
string getCandidate() const;
bool wasInPerson() const;
};
class Ballot {
private:
string voterID;
int votesStored;
Vote* votePointer[6];
public:
Ballot();
Ballot(string voterID2);
~Ballot();
string getVoterId() const;
int getVoteCount() const;
const Vote* getVote(int votePosition) const;
void recordVote(string office, string candidateName, bool voteInPerson);
int countInPersonVotes();
int findVote(string office) const;
};
//functions file
#include "p3.h"
#include <string>
using namespace std;
Election::Election(string office2, string firstCanidiateName2, string secondCanidiateName2) {
office = office2;
firstCanidiateName = firstCanidiateName2;
secondCanidiateName = secondCanidiateName2;
}
string Election::getOffice() const {
return office;
}
string Election::getCandidate1() const {
return firstCanidiateName;
}
string Election::getCandidate2() const {
return secondCanidiateName;
}
string Vote::getOffice() const {
return vOffice;
}
string Vote::getCandidate() const {
return canidiateName;
}
bool Vote::wasInPerson() const {
return voteMadeInPerson;
}
Vote::Vote(string vOffice2, string canidiateName2, bool voteMadeInPerson2) {
if (vOffice2.empty() == true) {
vOffice2 = "Unknown";
}
vOffice = vOffice2;
if (canidiateName2.empty() == true) {
canidiateName2 = "Write In";
}
canidiateName = canidiateName2;
voteMadeInPerson = voteMadeInPerson2;
}
string Ballot::getVoterId() const {
return voterID;
}
int Ballot::getVoteCount() const {
return votesStored;
}
const Vote* Ballot::getVote(int votePosition) const {
Vote* out;
if (votePosition > -1 && votePosition < votesStored) {
//unsure if this is proper syntax -- will need testing
out = votePointer[votePosition];
return out;
}
else {
out = nullptr;
return out;
}
}
void Ballot::recordVote(string office, string candidateName, bool voteInPerson) {
if ((votesStored + 1) < 6) {
int x = findVote(office);
if (x != -1) {
Vote* z = new Vote(office, candidateName, voteInPerson);
votePointer[votesStored] = z;
++votesStored;
}
}
}
int Ballot::countInPersonVotes() {
int count = 0;
for (int i = 0; i < votesStored; ++i) {
bool x = false;
x = votePointer[i]->wasInPerson();
if (x == true) {
count++;
}
}
return count;
}
int Ballot::findVote(string office) const {
//test to make sure this logically works
int i = 0;
bool matchCheck = false;
for (i = 0; i < votesStored; ++i) {
if (votePointer[i]->getOffice() == office) {
matchCheck = true;
return i + 1;
}
}
if (matchCheck == false) {
return -1;
}
}
Ballot::Ballot(string voterID2) {
voterID = voterID2;
votesStored = 0;
}
Ballot::Ballot() {
voterID = "Invalid ID";
votesStored = 0;
}
Ballot::~Ballot() {
/**this is what the prompt asks for, technically.
the book details that this can be achieved in a more simple matter --
using the delete[] operator.
*/
for (int i = 0; i < 6; ++i) {
delete votePointer[i];
}
}
Any help would be appreciated - I am unsure where I am messing up here. I seem to be getting a lot of read access violation errors, but we haven't touched on debugging, so I'm not necessarily sure how to identify what I did wrong. Furthermore, my functions did seem to work initially until I tried fixing it, and now it gives me a read-error almost instantly, which is concerning. And lastly - I am new to pointers, so there is a very high chance I messed up there. Thanks for any advice.
So I have to write a string class, and I need help with reading from a file into a vector of string classes I've created. It somewhat works, as it reads from the file but it repeats the the word read in a few times depending on which word it's on.
// .h
/*Class description:
A string class. Various functions for the class.
String is passed into objects of the class. Reads
and writes to files.*/
#ifndef MYString12_H
#define MYString12_H
#include <fstream>
using namespace std;
class MYString12
{
public:
MYString12();
MYString12(const MYString12 & mstr);
MYString12(const char* ptr);
~MYString12();
MYString12& operator = (const MYString12& argStr);
friend MYString12 operator + (const MYString12& str1, const MYString12& str2);
char operator [] (int index);
bool operator > (const MYString12& argStr2);
bool operator < (const MYString12& argStr2);
bool operator == (const MYString12& argStr);
friend istream& operator >> (istream& istr, MYString12& argStr);
friend ostream& operator << (ostream& istr, MYString12& argStr);
int length() const;
int capacity()const;
char at(int index);
const char* c_str()const;
static int getCurrentCount();
static int getCreatedCount();
private:
char* str;
int cap = 20;
int end;
const int compareTo(const MYString12& argStr);
static int currentCount;
static int createdCount;
};
#endif
Here is class cpp file
// MYString12.cpp
#include "stdafx.h"
#include "MYString12.h"
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cstdlib>
using namespace std;
int MYString12::createdCount = 0;
int MYString12::currentCount = 0;
// default constructor
MYString12::MYString12()
{
cap = 20;
end = 0;
str = new char[cap];
str[end] = '\0';
createdCount++;
currentCount++;
}
// copy constructor
MYString12::MYString12(const MYString12& mstr)
{
this->end = mstr.end;
this->cap = mstr.cap;
this->str = new char[mstr.cap];
while (end >= cap) {
cap += 20;
}
for (int i = 0; i < end; i++) {
str[i] = mstr.str[i];
}
//mstr.str[end] = '\0';
createdCount++;
currentCount++;
}
// constructor with string passed in
MYString12::MYString12(const char* ptr)
{
int i = 0;
while (ptr[i] != '\0') {
end++;
i++;
}
while (end >= cap) {
cap += 20;
}
str = new char[cap];
for (int j = 0; j < end; j++) {
str[j] = ptr[j];
}
createdCount++;
currentCount++;
}
// destructor
MYString12::~MYString12()
{
delete[] str;
currentCount--;
}
// overloaded assignment operator
GAString12& GAString12::operator = (const GAString12& mstr)
{
if (this == &mstr) {
return *this;
}
this->end = mstr.end;
this->cap = mstr.cap;
while (end >= cap) {
cap += 20;
}
for (int i = 0; i < end; i++) {
str[i] = mstr.str[i];
}
//mstr.str[end] = '\0';
return *this;
}
// overloaded concatanation operator
MYString12 operator + (const MYString12& str1, const MYString12& str2)
{
int temp = str1.end + str2.end + 1;
char tempArray[200];
int i = 0;
int j = 0;
while (i < temp)
{
if (i < str1.end)
{
tempArray[i] = str1.str[i];
i++;
} else {
tempArray[i] = str2.str[j];
i++;
j++;
}
}
tempArray[i] = '\0';
MYString12 concatenatedObj(tempArray);
return concatenatedObj;
}
// overloaded index operator
char MYString12::operator [] (int index)
{
return str[index];
}
// overloaded greater than operator
bool MYString12::operator > (const MYString12& argStr)
{
if ((*this).compareTo(argStr) > 0)
{
return true;
}
else {
return false;
}
}
// overloaded less than operator
bool MYString12::operator < (const MYString12& argStr)
{
if ((*this).compareTo(argStr) < 0)
{
return true;
}
else {
return false;
}
}
// overloaded equals equals operator
bool MYString12::operator == (const MYString12& argStr)
{
if ((*this).compareTo(argStr) == 0)
{
return true;
}
else {
return false;
}
}
// compares ascii values of objStr and argStr
const int MYString12::compareTo(const MYString12& argStr)
{
int asciiSubtraction = 0;
int limit = 0;
if (end <= argStr.end)
{
limit = end;
}
else {
limit = argStr.end;
}
int i = 0;
while (i <= limit && (str[i] == argStr.str[i])) {
i++;
}
asciiSubtraction = str[i] - argStr.str[i];
return asciiSubtraction;
}
// overloaded extraction operator
istream& operator >> (istream& istr, MYString12& argStr)
{
char temp[100];
istr >> temp;
argStr = GAString12(temp);
return istr;
}
// overloaded insertion operator
ostream& operator << (ostream& ostr, MYString12& argStr)
{
int i = 0;
while (argStr.str[i] != '\0')
{
ostr << argStr.str;
i++;
}
return ostr;
}
// returns size of passed in string
int MYString12::length() const
{
return end;
}
// returns size of memory allocated
int MYString12::capacity() const
{
return cap;
}
// returns a char of string at passed index
char MYString12::at(int index)
{
if (index < 0 || index > end) {
return '\0';
}
else {
return str[index];
}
}
// returns passed in string as c string
const char* MYString12::c_str() const
{
createdCount++;
currentCount++;
return str;
}
// returns the amount of alive instances of class
int MYString12::getCurrentCount()
{
return currentCount;
}
// returns the amount of overall created instances of class
int MYString12::getCreatedCount()
{
return createdCount;
}
And here is main
// main
int main()
{
vector<MYString12> word(100);
ifstream fin;
fin.open("infile3.txt");
if (fin.fail()) {
cout << "Error." << endl;
exit(1);
}
int wordCount = 0;
while (fin >> word[wordCount]) {
cout << word[wordCount];
system("pause");
wordCount++;
}
word.resize(wordCount);
fin.close();endl;
return 0;
}
It doesn't print out to the console any of the words. Nothing is printed. Why doesn't it print?
I was assigned to create a chained hash table using a vector of vectors. It is designed to hold objects of type Entry. I have written all of the functions and the constructor but when I try to use the constructor that reads an input and then output it back to me in order of keys, whichever string was on the last line of the .txt file its reading from is gone. I think its a problem with my constructor because when I try to use get to get that specific value from the table before printing it its gone. I was hoping for some insight to where I might be going wrong from someone with a little more experience. Thanks. Heres my code:
Entry Header File:
#ifndef entry_h
#define entry_h
// entry.h - defines class Entry
#include <string>
#include <iosfwd>
class Entry {
public:
// constructor
Entry(unsigned int key = 0, std::string data = "");
// access and mutator functions
unsigned int get_key() const;
std::string get_data() const;
static unsigned int access_count();
void set_key(unsigned int k);
void set_data(std::string d);
// operator conversion function simplifies comparisons
operator unsigned int () const;
// input and output friends
friend std::istream& operator>>
(std::istream& inp, Entry &e);
friend std::ostream& operator<<
(std::ostream& out, Entry &e);
private:
unsigned int key;
std::string data;
static unsigned int accesses;
};
#endif /* entry_h */
Table header file:
//
// table.h
//
//
#ifndef table_h
#define table_h
#include <string>
#include <vector>
#include <algorithm>
#include "entry.h"
using namespace std;
class Table {
public:
Table(unsigned int max_entries = 100);
//Builds empty table to hold 100 entries
Table(unsigned int entries, std::istream& input);
//Builds table to hold entries, reads and puts them 1 at a time
~Table();
//Destructor
void put(unsigned int key, std::string data);
//Creates new entry for the table
//Updates if key is used
void put(Entry e);
//Creates a copy of e in the table
string get(unsigned int key) const;
//Returns string associated with key
//Returns empty string if key isnt used
bool remove(unsigned int key);
//Removes entry in given key
//Returns true of removed, false of no entry
int find(Entry e);
//index in second array that e exists, 0 if not found
friend std::ostream& operator<< (std::ostream& out, const Table& t);
//Prints each entry in the table in order of their key
private:
int size;
int num_entries;
unsigned int hashkey(unsigned int key) const;
vector<vector<Entry> > A;
};
#endif /* table_h */
Table Implementation File:
//table.cpp
#include<iostream>
#include<vector>
#include<algorithm>
#include "table.h"
using namespace std;
Table::Table(unsigned int max_entries){
max_entries = 100;
size = max_entries * 2;
A.resize(size);
}
Table::Table(unsigned int entries, std::istream& input){
size = entries*2;
A.resize(size);
num_entries = entries;
Entry temp;
for (size_t i = 0; i < entries; i++) {
input >> temp;
put(temp);
}
}
Table::~Table() {
A.clear();
}
void Table::put(unsigned int key, std::string data){
Entry e;
e.set_key(key);
e.set_data(data);
put(e);
num_entries++;
}
void Table::put(Entry e) {
if (A[hashkey(e.get_key())].empty()) {
A[hashkey(e.get_key())].push_back(e);
}
else {
for(size_t i = 0; i < A[hashkey(e.get_key())].size(); i++) {
if (A[hashkey(e.get_key())][i].get_key() == e.get_key()) {
remove(A[hashkey(e.get_key())][i].get_key());
break;
}
}
A[hashkey(e.get_key())].push_back(e);
}
}
string Table::get(unsigned int key) const {
if( A[hashkey(key)].size() == 0) {
return "";
}
else {
for (size_t i = 0; i < A[hashkey(key)].size(); i++) {
if (A[hashkey(key)][i].get_key() == key) {
return A[hashkey(key)][i].get_data();
}
else {
return "";
}
}
}
}
bool Table::remove(unsigned int key) {
for (size_t i = 0; i < A[hashkey(key)].size(); i++) {
if (A[hashkey(key)][i].get_key() == key) {
swap(A[hashkey(key)][i],A[hashkey(key)][A[hashkey(key)].size() - 1]);
A[hashkey(key)].pop_back();
num_entries--;
return true;
}
else {
return false;
}
}
}
int Table::find(Entry e) {
for (size_t i = 0; i < A[hashkey(e.get_key())].size(); i++) {
if (A[hashkey(e.get_key())][i] == e) {
return i;
}
else {
return 0;
}
}
}
ostream& operator << (ostream& out, const Table& t) {
vector<Entry> order;
for(size_t i = 0; i < t.A.size(); i++) {
for (size_t j = 0; j < t.A[i].size(); j++) {
order.push_back(t.A[i][j]);
}
}
sort(order.begin(), order.end());
for(Entry k: order) {
out << k << endl;
}
return out;
}
unsigned int Table::hashkey(unsigned int key) const{
const double c = 1.61803;
// return key % size;
return (int)(size*((key * c) - (int)(key * c)));
}
im working on a little scrabblegame which i read a txtfile and create a hashtable for all words inside. Word is a specific class which contains a vector of .
i want to create a hashmap by my own and define the length of my "Dictionary" is 50000. im using a nullpointer to reserve all index of my array. If i want to print to my hashtable, compiler tells me a segmentation fault. does any one seems the error?
the headerfile:
class Dictionary {
public:
Dictionary();
Dictionary(string filepath);
friend std::ostream& operator<<(std::ostream& os, const Dictionary& obj);
bool find(const Word& word);
vector<Word> allPossibleWords(const vector<Character>& tiles);
// struct compare {
//
// bool operator()(const Word& a, const Word& b) {
// return a.operator<(b);
// }
// } myCompare;
vector<Word> m_allWords;
vector<Word>::iterator itVecWords;
static const int table_size = 500000;
// std::array <Word*, table_size> arrWords = {nullptr};
Word* arrWords[table_size] = {nullptr};
int hash(Word new_word);
void addItem(Word word);
void printHashTable();
the cpp:
Dictionary::Dictionary(string filepath) {
ifstream datei(filepath.c_str());
while (datei.good() && !datei.eof()) {
string temp;
string temp1;
string::size_type pos;
getline(datei, temp);
pos = temp.find(" ");
temp1 = temp.substr(0, pos);
Word new_word(temp1);
addItem(new_word);
}
datei.close();
}
std::ostream& operator<<(std::ostream& os, const Dictionary& obj) {
for (int i = 0; i < obj.m_allWords.size(); i++) {
os << obj.m_allWords[i] << endl;
}
return os;
}
bool Dictionary::find(const Word& word) const {
if (std::binary_search(m_allWords.begin(), m_allWords.end(), word)) {
return true;
}
return false;
}
vector<Word> Dictionary::allPossibleWords(const vector<Character>& tiles) const {
vector<Word> ergebnis;
string tmp;
int cnt = 0;
for (int i = 0; i < tiles.size(); i++) {
tmp += tiles[i].GetC();
}
sort(tmp.begin(), tmp.end());
for (int i = 1; i <= tiles.size(); i++) {
do {
string piece = tmp.substr(0, i);
do {
Word search = Word(piece);
//Überschreibt immer der in Ergebnis existierte Wert
if (find(search) && std::find(ergebnis.begin(), ergebnis.end(), search) == ergebnis.end()) {
ergebnis.push_back(search);
}
} while (next_permutation(piece.begin(), piece.end()));
} while (next_permutation(tmp.begin(), tmp.end()));
}
return ergebnis;
}
int Dictionary::hash(Word new_word) {
int index = 0;
for (auto u : new_word.new_Character) {
index += (int) u.GetC();
}
index = index * (int) new_word.new_Character.at(0).GetC();
index = index * (int) new_word.new_Character.at(new_word.new_Character.size() - 1).GetC();
return index % table_size;
}
void Dictionary::addItem(Word word) {
int index = hash(word);
if (arrWords[index] == nullptr) {
arrWords[index] = new Word(word);
} else {
Word* ptr = arrWords[index];
Word* neu = new Word(word);
while (ptr->getNextWord() != nullptr) {
ptr = ptr->getNextWord();
}
ptr->setNextWord(neu);
}
}
void Dictionary::printHashTable() {
Word* tmp;
for (int i = 0; i < table_size; i++) {
tmp = arrWords[i];
if (tmp != nullptr) {
tmp->printWord();
cout << "Index : " << i;
}
tmp = tmp->getNextWord();
}
}
class Word {
public:
Word();
Word(string m_wort);
int length() const;
int points() const;
friend std::ostream& operator<<(std::ostream& os, const Word& obj);
bool operator==(const Word& right) const; /
bool operator!=(const Word& right) const;
bool contains(const Character& c) const;
Word substr(int start, int end) const;
bool operator<(const Word& right) const;
vector <Character> new_Character;
Word* Next = nullptr;
void setNextWord(Word*);
Word* getNextWord();
void printWord();
string getWordAsString();
CPP File:
void Word::setNextWord(Word* w) {
Next = w;
}
Word* Word::getNextWord() {
return Next;
}
void Word::printWord() {
string s = "";
for (int i = 0; i < new_Character.size(); i++) {
s += new_Character.at(i).GetC();
}
cout << s << endl;
}
string Word::getWordAsString() {
string s;
for (int i = 0; i < new_Character.size(); i++) {
s += new_Character.at(i).GetC();
}
return s;
}
In the code shown below, in the function void printExpensiveThanT(..) i'm supposed to print out the destination, distance and the price for the offers which are more expensive than the offer T in the function, sorted in ascending order by the distance value.
I'm not sure what should i use to sort them, i experimented something with vectors but it didn't work out so i deleted it.
Any help would be appreciated.
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
class Transport {
protected:
char destination[100];
int basePrice;
int distance;
public:
Transport() {}
Transport(char *destination, int basePrice, int distance) {
strcpy(this->destination, destination);
this->basePrice = basePrice;
this->distance = distance;
}
virtual ~Transport() {}
virtual int priceTransport() = 0;
friend bool operator<(const Transport &t1, const Transport &t2) {
return t1.distance<t2.distance;
}
int getDistance(){ return distance; }
char *getDestination() { return destination; }
int getPrice() { return basePrice; }
};
class AutomobileTransport : public Transport {
private:
bool ifDriver;
public:
AutomobileTransport() {}
AutomobileTransport(char *destination, int basePrice,int distance, bool ifDriver) : Transport(destination,basePrice,distance) {
this->ifDriver = ifDriver;
}
void setIfDriver(bool ifDriver) {
this->ifDriver = ifDriver;
}
bool getIfDriver() {
return ifDriver;
}
int priceTransport() {
if(ifDriver) {
basePrice+=basePrice*20/100;
}
return basePrice;
}
friend bool operator<(const AutomobileTransport &a1, const AutomobileTransport &a2) {
return a1.distance<a2.distance;
}
};
class VanTransport: public Transport {
private:
int passengers;
public:
VanTransport() {}
VanTransport(char *destination, int basePrice, int distance, int passengers) : Transport(destination, basePrice, distance) {
this->passengers = passengers;
}
void setPassengers(int passengers) {
this->passengers = passengers;
}
int getPassengers() {
return passengers;
}
int priceTransport() {
for(int i = 0; i < passengers; i++) {
basePrice-=200;
}
return basePrice;
}
friend bool operator<(const VanTransport &k1, const VanTransport &k2) {
return k1.distance<k2.distance;
}
};
void printExpensiveThanT(Transport **offers,int n,AutomobileTransport &T) {
Transport *tmp;
for(int i = 0; i <= n; i++){
if(offers[i]->priceTransport() > T.priceTransport())
cout<<offers[i]->getDestination()<<" "<<offers[i]->getDistance()<<" "<<offers[i]->getPrice()<<endl;
}
}
int main() {
char destination[20];
int type,price,distance,passengers;
bool driver;
int n;
cin>>n;
Transport **offers;
offers=new Transport *[n];
for (int i=0; i<n; i++) {
cin>>type>>destination>>price>>distance;
if (type==1) {
cin>>driver;
offers[i]=new AutomobileTransport(destination,price,distance,driver);
} else {
cin>>passengers;
offers[i]=new VanTransport(destination,price,distance,passengers);
}
}
AutomobileTransport at("Ohrid",2000,600,false);
printExpensiveThanT(offers,n,at);
for (int i=0; i<n; i++) delete offers[i];
delete [] offers;
return 0;
}
Since you're dealing with pointers, the easiest thing to do is to use std::vector and std::sort:
#include <vector>
//...
void printExpensiveThanT(Transport **offers, int n, AutomobileTransport &T)
{
std::vector<Transport*> sortedVect;
for (int i = 0; i < n; i++)
{
if (offers[i]->priceTransport() > T.priceTransport())
sortedVect.push_back(offers[i]); // add this item to the vector
}
// sort the vector based on the dereferenced pointers and their respective
// operator <
std::sort(sortedVect.begin(), sortedVect.end(),
[](Transport* left, Transport* right) { return *left < *right; });
// print out the values
for (auto it : sortedVect)
cout << (*it).getDestination() << " " << (*it).getDistance() << " " << (*it).getPrice() << "\n";
}
Also, your original code looped one more than it should (i <= n was wrong).
Edit:
If your compiler doesn't support the C++ 11 syntax, here is an alternate solution:
#include <vector>
//...
bool Sorter(Transport* left, Transport* right)
{ return *left < *right; }
void printExpensiveThanT(Transport **offers, int n, AutomobileTransport &T)
{
std::vector<Transport*> sortedVect;
for (int i = 0; i < n; i++)
{
if (offers[i]->priceTransport() > T.priceTransport())
sortedVect.push_back(offers[i]); // add this item to the vector
}
// sort the vector based on the dereferenced pointers and their respective
// operator <
std::sort(sortedVect.begin(), sortedVect.end(), Sorter);
// print out the values
std::vector<Transport*>::iterator it = sortedVect.begin();
while (it != sortedVect.end())
{
cout << (*it).getDestination() << " " << (*it).getDistance() << " " << (*it).getPrice() << "\n";
++it;
}
}