C++ Compliler Problems

Hello all together , I had to make a task and have some compiler errors

Header
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
#ifndef CTEAMTABLE_H_
#define CTEAMTABLE_H_
#include<iostream>
#include "CTeam.h"
#include <stdio.h>
#include <string.h>
using namespace std;

class CTeamTable {
private:
	CTeam* m_pTable;
	unsigned int m_maxEntry;
	unsigned int m_curEntry;

public:
	CTeamTable(int unsigned maxEntry);
	~CTeamTable();
	bool addTeam(CTeam const& team);
	void print(string headline);
	bool addResult(string team1,string team2,unsigned int goalsTeam1, unsigned int goalsTeam2);
	void sort();


};

#endif /* CTEAMTABLE_H_ */ 




cpp

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
 

#include "CTeamTable.h"
#include "CTeam.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

CTeamTable::CTeamTable(int unsigned maxEntry = 10){
	if (maxEntry < 5){
		maxEntry = 5;
	}
    m_maxEntry = maxEntry;
    m_curEntry = 0;
    m_pTable = new CTeam[m_maxEntry];

}

CTeamTable::~CTeamTable(){
	delete[] m_pTable;
}

bool CTeamTable::addTeam(CTeam const& team) {
	if (m_curEntry >= m_maxEntry) {
		return false;
	}
	m_pTable[m_curEntry++] = team;
	return true;
}

void CTeamTable::print(string headline) {
	cout << headline << endl;
	for (int i = 0; i < m_curEntry; i++) {
   		cout << i+1 << " - " << m_pTable[i];
	}
	cout << endl;
}

void CTeamTable::sort() {
	for (int i = 0; i < m_curEntry; i++) {
		for (int j = 0; j < m_curEntry - 1; j++) {
			if (m_pTable[j+1] < m_pTable[j]) {
				CTeam tmp = m_pTable[j];
				m_pTable[j] = m_pTable[j+1];
				m_pTable[j+1] = tmp;
			}
		}
	}
}
    CTeam t1 = NULL;
	CTeam t2 = NULL;
bool CTeamTable::addResult(string team1,string team2,unsigned int goalsTeam1, unsigned int goalsTeam2){

	for (int i = 0; i < m_maxEntry; i++) {
		if (strcmp(m_pTable[i].m_teamName, team1) == 0) {
			t1 = m_pTable[i];
		}
		if (strcmp(m_pTable[i].m_teamName, team2) == 0) {
			t2 = m_pTable[i];
		}
	}
	if (t1 == NULL || t2 == NULL) return false; //Team nicht gefunden
	t1.addResult(goalsTeam1, goalsTeam2);
	t2.addResult(goalsTeam2, goalsTeam1);
	return true;

	}

 


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
#include "CTeamTable.h"
#include "CTeam.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

int main2(){

	CTeam bayern("Bayern München");
		CTeam bvb("Borussia Dortmund");
		CTeam werder("Werder Bremen");
		bayern.addResult(3,3);
		bayern.addResult(1,4);
		werder.addResult(3,3);
		bvb.addResult(4,1);
		CTeamTable table(3);
		table.addTeam(bayern);
		table.addTeam(werder);
		table.addTeam(bvb);
		table.print("Unsortierte Tabelle");
		table.sort();
		table.print("Sortierte Tabelle");

	table.addResult("Borussia Drotmund", "Werder Bremen", 1, 0);
}

  

Description Resource Path Location Type
cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)' CTeamTable.cpp /CTeam line 64 C/C++ Problem

Description Resource Path Location Type
conversion from 'int' to non-scalar type 'CTeam' requested CTeamTable.cpp /CTeam line 56 C/C++ Problem

Description Resource Path Location Type
conversion from 'int' to non-scalar type 'CTeam' requested CTeamTable.cpp /CTeam line 57 C/C++ Problem

Description Resource Path Location Type
no match for 'operator==' (operand types are 'CTeam' and 'int') CTeamTable.cpp /CTeam line 68 C/C++ Problem

Description Resource Path Location Type
within this context CTeamTable.cpp /CTeam line 61 C/C++ Problem

Description Resource Path Location Type
within this context CTeamTable.cpp /CTeam line 64 C/C++ Problem


Can somebody help me to solve the Compiler problems ?

I use Eclipse
std::strcmp is for C strings (char*). For std::string you can simply use the == operator to compare them.
But in the task was written to compare the strings .

What should I include so that it works?

For the string comparison simply use == like you do when comparing int, double and other simple data types.

 
if (m_pTable[i].m_teamName == team1) {

Note that if you use std::string you should include the <string> header.

I'm not sure about the other errors. The line numbers in the error message doesn't match the code you have posted. If you are still getting errors can you please point out which lines they're on.
Last edited on
1
2
3
4
CTeam t1 = NULL; // conversion from 'int' to non-scalar type 'CTeam' requested
CTeam t2 = NULL; // conversion from 'int' to non-scalar type 'CTeam' requested
if ( t1 == NULL || t2 == NULL ) // no match for 'operator==' (operand types are 'CTeam' and 'int')
  return false;

NULL is a preprocessor macro. Your code is essentially:
1
2
3
CTeam t1 = 0;
CTeam t2 = 0;
if ( t1 == 0 || t2 == 0 ) return false;

You did not show definition of CTeam. What does it mean to have a "null" CTeam?
This is my CTeam
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
#pragma once
#include <string>
#include <iostream>
using namespace std;


class CTeam{

private:
	string m_teamName;
	unsigned int m_winsTotal;
	unsigned int m_equalTotal;
	unsigned int m_lostTotal;
	unsigned int m_goalsScoredTotal;
	unsigned int m_goalsReceivedTotal;

public:

	CTeam(string name = "noName");
	string getName();
	void addResult( unsigned int goalsScored, unsigned int goalsReceived);
    unsigned int getTotalPoints() const;

    friend ostream& operator << (ostream& out , CTeam const& rop);
    bool operator < (const CTeam& rop) const;
};



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

#include <iostream>
#include <string>
#include "CTeam.h"

using namespace std;

CTeam::CTeam(string name) {
    m_teamName = name;
	m_goalsScoredTotal = 0;
	m_goalsReceivedTotal = 0;
	m_winsTotal = 0;
	m_equalTotal = 0;
	m_lostTotal = 0;
}

string CTeam::getName() {
    return m_teamName;
}


void CTeam::addResult(unsigned int goalsScored, unsigned int goalsReceived){
    if (goalsScored > goalsReceived) {
        m_winsTotal++;
    }
    if (goalsReceived > goalsScored) {
        m_lostTotal++;
    }
    if (goalsReceived == goalsScored) {
           m_equalTotal++;
    }
	m_goalsScoredTotal += goalsScored;
	m_goalsReceivedTotal+= goalsReceived;
 };

unsigned int CTeam::getTotalPoints() const {
	  return  m_winsTotal*3 + m_equalTotal*1;
};

ostream& operator << (ostream &out, const CTeam &rop) {
	 out<<"CTeam@" << rop.m_winsTotal<< "Spiele gewonnen  " << " , " <<  rop.m_equalTotal << " Spiele unentschieden"<< endl;

	 out << "CTeam@"<<  rop.m_goalsScoredTotal << " Tore erzielt " <<" , " << rop.m_goalsReceivedTotal << " " << " Tore kassiert "<< endl;
};
bool CTeam::operator < (const CTeam& rop) const {
	//es wird true zurückgegeben, wenn der linke Operand mehr Punkte hat als der rechte Operand
	if (this->getTotalPoints() > rop.getTotalPoints()) return true;

	//bei gleich vielen Punkten entscheidet die Tordifferenz
	if (this->getTotalPoints() == rop.getTotalPoints()) {
		int team1Diff = this->m_goalsScoredTotal - this->m_goalsReceivedTotal;
		int team2Diff = rop.m_goalsScoredTotal - rop.m_goalsReceivedTotal;
		//Wenn der linke Operand die bessere (höhere) Tordifferenz hat als der rechte, wird ebenfalls true zurückgegeben
		if (team1Diff > team2Diff) return true;
	}

	//in allen anderen Fällen wird false zurückgegeben
	return false;
}


You can create CTeam objects by three ways:
1
2
3
4
5
6
7
8
9
10
11
// #1
CTeam t1( "foo" ); // pass name to constructor, the std::string argument is initialized with constant string literal

// #2
CTeam t2; // Same as writing CTeam t2( "noName" );

// #3
CTeam t4 = t1; // uses implicitly created copy constructor

// #4
// move construction, introduced in C++11 

If no valid CTeam has name "noName", then "noName" == t1.getName() is true for "invalid" team.


Why are your t1 and t2 global variables?
I corrected it now .

I have only two errors now.

#pragma once
#include <string>
#include <iostream>
using namespace std;


class CTeam{

private:
string m_teamName;
unsigned int m_winsTotal;
unsigned int m_equalTotal;
unsigned int m_lostTotal;
unsigned int m_goalsScoredTotal;
unsigned int m_goalsReceivedTotal;

public:

CTeam(string name = "noName");
string getName();
void addResult( unsigned int goalsScored, unsigned int goalsReceived);
unsigned int getTotalPoints() const;

friend ostream& operator << (ostream& out , CTeam const& rop);
bool operator < (const CTeam& rop) const;
};



#include <iostream>
#include <string>
#include "CTeam.h"

using namespace std;

CTeam::CTeam(string name) {
m_teamName = name;
m_goalsScoredTotal = 0;
m_goalsReceivedTotal = 0;
m_winsTotal = 0;
m_equalTotal = 0;
m_lostTotal = 0;
}

string CTeam::getName() {
return m_teamName;
}


void CTeam::addResult(unsigned int goalsScored, unsigned int goalsReceived){
if (goalsScored > goalsReceived) {
m_winsTotal++;
}
if (goalsReceived > goalsScored) {
m_lostTotal++;
}
if (goalsReceived == goalsScored) {
m_equalTotal++;
}
m_goalsScoredTotal += goalsScored;
m_goalsReceivedTotal+= goalsReceived;
};

unsigned int CTeam::getTotalPoints() const {
return m_winsTotal*3 + m_equalTotal*1;
};

ostream& operator << (ostream &out, const CTeam &rop) {
out << rop.m_teamName << " : " << rop.m_winsTotal << " " << rop.m_equalTotal << " " << rop.m_lostTotal << " ( " << rop.getTotalPoints() << " Punkte ) Tore " << rop.m_goalsScoredTotal << ":" << rop.m_goalsReceivedTotal << endl;
};
bool CTeam::operator < (const CTeam& rop) const {
//es wird true zurückgegeben, wenn der linke Operand mehr Punkte hat als der rechte Operand
if (this->getTotalPoints() > rop.getTotalPoints()) return true;

//bei gleich vielen Punkten entscheidet die Tordifferenz
if (this->getTotalPoints() == rop.getTotalPoints()) {
int team1Diff = this->m_goalsScoredTotal - this->m_goalsReceivedTotal;
int team2Diff = rop.m_goalsScoredTotal - rop.m_goalsReceivedTotal;
//Wenn der linke Operand die bessere (höhere) Tordifferenz hat als der rechte, wird ebenfalls true zurückgegeben
if (team1Diff > team2Diff) return true;
}

//in allen anderen Fällen wird false zurückgegeben
return false;
}

#ifndef CTEAMTABLE_H_
#define CTEAMTABLE_H_
#include<iostream>
#include "CTeam.h"

using namespace std;

class CTeamTable {
private:
CTeam* m_pTable;
unsigned int m_maxEntry;
unsigned int m_curEntry;

public:
CTeamTable(int unsigned maxEntry);
~CTeamTable();
bool addTeam(CTeam const& team);
void print(string headline);
bool addResult(string team1,string team2,unsigned int goalsTeam1, unsigned int goalsTeam2);
void sort();


};

#endif /* CTEAMTABLE_H_ */



#include "CTeamTable.h"
#include "CTeam.h"
#include <string>
#include <iostream>
using namespace std;

CTeamTable::CTeamTable(int unsigned maxEntry = 10){
if (maxEntry < 5){
maxEntry = 5;
}
m_maxEntry = maxEntry;
m_curEntry = 0;
m_pTable = new CTeam[m_maxEntry];

}

CTeamTable::~CTeamTable(){
delete[] m_pTable;
}

bool CTeamTable::addTeam(CTeam const& team) {
if (m_curEntry >= m_maxEntry) {
return false;
}
m_pTable[m_curEntry++] = team;
return true;
}

void CTeamTable::print(string headline) {
cout << headline << endl;
for (int i = 0; i < m_curEntry; i++) {
cout << i+1 << " - " << m_pTable[i];
}
cout << endl;
}

void CTeamTable::sort() {
for (int i = 0; i < m_curEntry; i++) {
for (int j = 0; j < m_curEntry - 1; j++) {
if (m_pTable[j+1] < m_pTable[j]) {
CTeam tmp = m_pTable[j];
m_pTable[j] = m_pTable[j+1];
m_pTable[j+1] = tmp;
}
}
}
}

bool CTeamTable::addResult(string team1,string team2,unsigned int goalsTeam1, unsigned int goalsTeam2) {
CTeam* t1 = nullptr; here is an errror ................
CTeam* t2 = nullptr;
for (int i = 0; i < m_maxEntry; i++) {
if (team1.compare(m_pTable[i].getName()) == 0) {
t1 = &m_pTable[i];
}
if (team2.compare(m_pTable[i].getName()) == 0) {
t2 = &m_pTable[i];
}
}
if (t1 == nullptr || t2 == nullptr) return false; //Team nicht gefunden
t1->addResult(goalsTeam1, goalsTeam2);
t2->addResult(goalsTeam2, goalsTeam1);
return true;
}

I marked the error


Description Resource Path Location Type
'nullptr' was not declared in this scope CTeamTable.cpp /CTeam2 line 50 C/C++ Problem

Description Resource Path Location Type
make: *** [CTeamTable.o] Error 1 CTeam2 C/C++ Problem
So simply remove the = NULL after t1/t2.

"noName" is usually not a good name. Why do you thing you need that? You may just leave it empty "".

To indicate a valid team you may check for an empty name:

if (t1.getName().empty() || t2.getName().empty()) return false; //Team nicht gefunden

This is not Java/C#. You do not have pointer if you do not explicitely create one.
Like so:

CTeam *t1 = 0; // Note: * for a pointer (you can set to null)
Last edited on
'nullptr' was not declared in this scope

nullptr is a keyword that was introduced in C++11.
http://en.cppreference.com/w/cpp/language/nullptr

Have you set your compiler to support C++11 (or newer) features?
(Some compilers default to older standard.)
Topic archived. No new replies allowed.