Hi I have this c++ project which gets me some weird error which I have no idea how to fix. So if someone can give me a solution, that would be great.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
using std::ostream;
using std::endl;
using std::cout;
class CStudentEmploy
{
private:
string m_strName;
string m_strFacNum;
int m_iMinutes;
public:
CStudentEmploy(int m = 0) // Podrazbirasht se konstruktor
{
m_strName = "N/A";
m_strFacNum = "N/A";
m_iMinutes = m;
}
CStudentEmploy(string n, string f, int m) // Ekspliciten konstruktor
{
m_strName = n;
m_strFacNum = f;
m_iMinutes = m;
}
CStudentEmploy(const CStudentEmploy &obj) // Copy konstruktor
{
m_strName = obj.m_strName;
m_strFacNum = obj.m_strFacNum;
m_iMinutes = obj.m_iMinutes;
}
string GetName()
{
return m_strName;
}
string GetFacNum()
{
return m_strFacNum;
}
int GetMinutes()
{
return m_iMinutes;
}
void SetName (string n)
{
m_strName = n;
}
void SetFacNum (string f)
{
m_strFacNum = f;
}
void SetMinutes (int m)
{
m_iMinutes = m;
}
CStudentEmploy operator =(CStudentEmploy obj)
{
m_strName = obj.m_strName;
m_strFacNum = obj.m_strFacNum;
m_iMinutes = obj.m_iMinutes;
return *this;
}
bool operator < (const CStudentEmploy& obj) const
{
return m_iMinutes < obj.m_iMinutes;
}
CStudentEmploy operator +(const CStudentEmploy &obj) const
{
return CStudentEmploy(m_iMinutes + obj.m_iMinutes);
}
friend ostream& operator <<(ostream& os, CStudentEmploy &obj);
friend istream& operator>>(istream& is, CStudentEmploy &obj);
};
ostream& operator<<(ostream& os, CStudentEmploy &obj) {
os<<setiosflags(ios::left)<<setw(8)<<obj.GetName()
<<"|"<<setw(11)<<obj.GetFacNum()
<<"|"<<setw(8)<<obj.GetMinutes()<<endl;
return os;
}
istream& operator>>(istream& is, CStudentEmploy &obj) {
string tmp_strName;
string tmp_strFacNum;
int tmp_iMinutes;
is >> tmp_strName >> tmp_strFacNum >> tmp_iMinutes;
obj.SetName(tmp_strName);
obj.SetFacNum(tmp_strFacNum);
obj.SetMinutes(tmp_iMinutes);
return is;
}
class CAnalizeTime {
private:
vector<CStudentEmploy>m_vData;
void add(CStudentEmploy employ) {
m_vData.push_back(employ);
}
public:
CStudentEmploy getEmployAt(int i)
{
return m_vData[i];
}
long getEmployCount()
{
return m_vData.size();
}
CAnalizeTime()
{
ifstream fs;
fs.open("test.txt");
if(!fs.is_open()) cout<<"error opening file!\n";
CStudentEmploy employ;
while(!fs.eof())
{
fs>>employ;
add(employ);
}
}
CAnalizeTime(const string& strFileName)
{
ifstream fs;
fs.open(strFileName.c_str());
if(!fs.is_open()) cout<<"error opening file!\n";
CStudentEmploy employ;
while(!fs.eof())
{
fs>>employ;
add(employ);
}
}
void Sort()
{
sort(m_vData.begin(),m_vData.end());
}
vector<int> calcNums(int iR1,int iR2,int iR3,
int iR4,int iR5,int iR6)
{
vector<int> resultVector;
for (int i=0;i<5;i++)
{
resultVector.push_back(0);
}
for (i=0;i<m_vData.size();i++)
{
if(m_vData[i].GetMinutes()>=iR1
&&m_vData[i].GetMinutes()<iR2)
resultVector[0]++;//[iR1-iR2)
if(m_vData[i].GetMinutes()>=iR2
&&m_vData[i].GetMinutes()<iR3)
resultVector[1]++;//[iR2-iR3)
if(m_vData[i].GetMinutes()>=iR3
&&m_vData[i].GetMinutes()<iR4)
resultVector[2]++;//[iR3-iR4)
if(m_vData[i].GetMinutes()>=iR4
&&m_vData[i].GetMinutes()<iR5)
resultVector[3]++;//[iR4-iR5)
if(m_vData[i].GetMinutes()>=iR5
&&m_vData[i].GetMinutes()<iR6)
resultVector[4]++;//[iR5-iR6)
}
return resultVector;
}
double calcMean()
{
double sum=0;
for (int i=0;i<m_vData.size();i++)
{
sum+=m_vData[i].GetMinutes();
}
return sum/m_vData.size();
}
};
ostream& operator<<(ostream& os, CAnalizeTime &obj)
{
for (int i=0;i<obj.getEmployCount();i++)
{
cout<<obj.getEmployAt(i);
}
return os;
}
void main()
{
CAnalizeTime myTimeAnalyzer;
//myTimeAnalyzer.Sort();
cout<<setiosflags(ios::left)<<setw(8)
<<"Name"<<setw(12)
<<"|FacNum"<<setw(8)<<"|Minutes"<<endl;
cout<<"----------------------------"<<endl;
cout<<myTimeAnalyzer;
cout<<"CalcMean result:"<<myTimeAnalyzer.calcMean()<<endl;
vector<int>myCalcNums = myTimeAnalyzer.calcNums(1,50,100,200,400,800);
cout<<"CalcNums result:"
<<myCalcNums[0]<<","
<<myCalcNums[1]<<","
<<myCalcNums[2]<<","
<<myCalcNums[3]<<","
<<myCalcNums[4]<<endl;
system("pause");
return;
}
I'm getting this error when I run it on VC6 error C2593: 'operator >>' is ambiguous, and I get this error when I run it on 2010
1>------ Build started: Project: test_project, Configuration: Debug Win32 ------
1>Build started 5/14/2013 10:02:29 PM.
1>InitializeBuildStatus:
1> Touching "Debug\test_project.unsuccessfulbuild".
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>C:\Users\User\Desktop\test_project\test_project\Debug\test_project.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.80
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
My question is what is causing this errors
I have the errors here on these lines:
CAnalizeTime()
{
ifstream fs;
fs.open("test.txt");
if(!fs.is_open()) cout<<"error opening file!\n";
CStudentEmploy employ;
while(!fs.eof())
{
fs>>employ;
add(employ);
}
}
CAnalizeTime(const string& strFileName)
{
ifstream fs;
fs.open(strFileName.c_str());
if(!fs.is_open()) cout<<"error opening file!\n";
CStudentEmploy employ;
while(!fs.eof())
{
fs>>employ;
add(employ);
}
}
This is what the error message looks like:
--------------------Configuration: project_testing - Win32 Debug--------------------
Compiling...
project_testing.cpp
D:\My Documents\project_testing\project_testing\project_testing.cpp(143) : error C2593: 'operator >>' is ambiguous
D:\My Documents\project_testing\project_testing\project_testing.cpp(156) : error C2593: 'operator >>' is ambiguous
Error executing cl.exe.
project_testing.obj - 2 error(s), 0 warning(s)
The problem is that obj.getEmployAt(i); returns a rvalue temporary CStudentEmploy object which you send to your operator<< overlaod. But the operator overload expects a reference and can not bind to an rvalue.
You will have to take a const reference instead
ostream& operator<<(ostream& os, const CStudentEmploy &obj) {
^^^^^
and fix the functions used in it as const for example
string GetName() const { return m_strName; }
^^^^^
Or you can fix your getEmployAt function to return a reference instead.
CStudentEmploy& getEmployAt(int i) { return m_vData[i]; }
^^^
As mentioned in the comments: I don't know what compiler you are using but void is not a valid return value for main. use int main and return 0;
Related
I have overloaded all the operators properly but the relational operators are giving me error while using them with cout . i have tried alot, it gives this error:
[Error] no match for 'operator<<' (operand types are 'GrandInt' and '<unresolved overloaded function type>')
#include <iostream>
#include<string.h>
using namespace std;
class GrandInt{
string a;
public:
GrandInt(){
a="";
}
GrandInt(long long unsigned int n){
a=to_string(n);
}
GrandInt(string n){
a=n;
}
GrandInt(const GrandInt &x){
a=x.a;
}
GrandInt operator+(GrandInt &x){
char *ap= new char [a.length()+1];
int al=a.length();
strcpy(ap,a.c_str());
char *xp= new char [x.a.length()+1];
int xl=x.a.length();
strcpy(xp,x.a.c_str());
for(;*ap!='\0';ap++){}
for(;*xp!='\0';xp++){}
int rl;
if(xl>al)
rl=xl+1;
else
rl=al+1;
char *r=new char [rl+1];
ap--;xp--;
int ac,xc,sum,cary=0,c=0;
for(;al>=0||xl>=0;al--,xl--,ap--,xp--,r++,c++){
if(al>0)
ac=*ap-48;
else
ac=0;
if(xl>0)
xc=*xp-48;
else
xc=0;
sum=ac+xc+cary;
if(sum>9){
cary=sum/10;
sum=sum%10;
}
else
cary=0;
*r=sum+48;
}
r--;
if(*r=='0')
{ r--;c--;}
GrandInt temp;
for(;c>0;c--,r--){
temp.a=temp.a+*r;
}
return temp;
}
GrandInt operator-(GrandInt &x){
char *ap= new char [a.length()+1];
int al=a.length();
strcpy(ap,a.c_str());
char *xp= new char [x.a.length()+1];
int xl=x.a.length();
strcpy(xp,x.a.c_str());
for(;*ap!='\0';ap++){}
for(;*xp!='\0';xp++){}
int rl=al;
char *r=new char [rl+1];
ap--;xp--;
int ac,xc,sum,cary=0,c=0;
for(;al>=0||xl>=0;al--,xl--,ap--,xp--,r++,c++){
if(xl>0)
xc=*xp-48;
else
xc=0;
ac=*ap-48;
if(ac<xc){
cary=10;
ap--;
*ap=*ap-1;
ap++;
}
sum=ac-xc+cary;
*r=sum+48;
cary=0;
}
r--;
if(*r=='0')
{ r--;c--;}
GrandInt temp;
for(;c>0;c--,r--){
temp.a=temp.a+*r;
}
return temp;
}
bool operator>(GrandInt &x){
if(a.compare(x.a)>0)
return 0;
else
return 1;
}
friend ostream &operator <<(ostream &out,GrandInt x){
cout<<x.a;
return out;
}
friend istream &operator>>(istream &in, GrandInt x){
cin>>x.a;
return in;
}
};
int main()
{
//starting with small numbers
GrandInt num1("546");
GrandInt num2("60");
cout<<num1+num2<<endl;//606
cout<<num1-num2<<endl;//486
cout<<num1>num2<<endl;//1
}```
You are not using the ostream& out in the overloaded operators. Try this:
friend ostream &operator<<(ostream &out, GrandInt x) {
out << x.a;
return out;
}
friend istream &operator>>(istream &in, GrandInt x) {
in >> x.a;
return in;
}
And in the main function:
cout<< (num1>num2) << endl; // parenthesis needed because of operator precedence
Hi guys I can not solve in the following error that I find in my code:
The error indicated by the IDE refers to the move constructor. The IDE does not report me errors are when I compile it gives me error.
Scanning dependencies of target Lab_01
[ 33%] Building CXX object CMakeFiles/Lab_01.dir/main.cpp.obj
In file included from C:\Users\alex\CLionProjects\LabPDS\Lab_01\main.cpp:2:
C:\Users\alex\CLionProjects\LabPDS\Lab_01\Message.h:21: error: expected `,' or `...' before '&&' token
C:\Users\alex\CLionProjects\LabPDS\Lab_01\Message.h:21: error: invalid constructor; you probably meant `Message (const Message&)'
C:\Users\alex\CLionProjects\LabPDS\Lab_01\Message.h:23: error: **expected `,' or `...' before '&&' token**
mingw32-make.exe[3]: *** [CMakeFiles/Lab_01.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Lab_01.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Lab_01.dir/rule] Error 2
mingw32-make.exe: *** [Lab_01] Error 2
Can someone help me?
my class main.cpp in line:2 find it the error, in the class Message.h
#include <iostream>
#include "Message.h"
int main() {
Message m1(10);
std::cout << m1 << std::endl;
return 0;
}
File: Message.h
// Created by Alexandro Vassallo on 10/04/2020.
#ifndef LAB_01_MESSAGE_H
#define LAB_01_MESSAGE_H
class Message {
long id;
char* data;
int size;
static long sId;
char* mkMessage(int n);
public:
Message(); //Costruttore di Default
Message(int n); //Costruttore con un solo parametro
~Message(); //Distruttore
Message(const Message& source); //Costruttore di copia
Message(Message&& source); //Costruttore di movimento
Message& operator=(const Message& source); //Operatore di assegnazione
Message& operator=(Message&& source); //Operatore di assegnazione di movimento
long getId() const;
void setId(long id);
char *getData() const;
void setData(char *data);
int getSize() const;
void setSize(int size);
static long getSId();
static void setSId(long sId);
};
std::ostream& operator<<(std::ostream& out, const Message& m);
#endif //LAB_01_MESSAGE_H
In line 21 and line 23 above there is the move constructor and the move assigment operator.
File: Message.cpp
// Created by Alexandro Vassallo on 10/04/2020.
#include <string>
#include <iostream>
#include "Message.h"
long Message::sId = 0;
Message::Message(): id(-1), size(0){
this->data = mkMessage(0);
}
Message::Message(int n): size(n) {
this->id= sId++;
this->data = mkMessage(n);
}
Message::~Message() {
delete[] data;
}
Message::Message(const Message &source):id(source.id), size(source.size) {
this->data = new char[size];
memcpy(this->data,source.data,size);
}
Message::Message(Message &&source) {
this->id = source.id;
this->size = source.size;
this->data = source.data;
source.id = -1;
source.size = 0;
source.data = nullptr;
}
Message &Message::operator=(const Message &source) {
if(this != &source){
delete[] this->data;
this->data = nullptr;
this->size = source.size;
this->data = new char[size];
memcpy(this->data, source.data,size);
}
return *this;
}
Message &Message::operator=(Message &&source) {
if(this != &source){
delete[] this->data;
this->size = source.size;
this->data = source.data;
source.data = nullptr;
}
return *this;
}
long Message::getId() const {
return id;
}
void Message::setId(long id) {
Message::id = id;
}
char *Message::getData() const {
return data;
}
void Message::setData(char *data) {
Message::data = data;
}
int Message::getSize() const {
return size;
}
void Message::setSize(int size) {
Message::size = size;
}
long Message::getSId() {
return sId;
}
void Message::setSId(long sId) {
Message::sId = sId;
}
char* Message::mkMessage(int n) {
std::string vowels ="aeiou";
std::string consolants = "bcdfghlmnqrstvz";
char* m = new char[n+1];
for(int i = 0 ; i < n ; i++){
m[i] = i%2 ? vowels[rand()%vowels.size()]:consolants[rand()%consolants.size()];
}
m[n] = 0;
return m;
}
std::ostream& operator<<(std::ostream& out, const Message& m){
out << m.getId() << m.getSize() << m.getData();
return out;
}
Make sure you are using C++11 or later. Move semantics are not supported by older versions.
I have a Student class and a Name class defined in header files with function implementation in cpp files. When compiling with Visual Studio 2015 I get the following errors:
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Student const &)" (??6#YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV01#ABVStudent###Z) referenced in function _main 3512-lab8 c:\Users\Tess\documents\visual studio 2015\Projects\3512-lab8\3512-lab8\main.obj 1
Error LNK2019 unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Student &)" (??5#YAAAV?$basic_istream#DU?$char_traits#D#std###std##AAV01#AAVStudent###Z) referenced in function _main 3512-lab8 c:\Users\Tess\documents\visual studio 2015\Projects\3512-lab8\3512-lab8\main.obj 1
Error LNK1120 2 unresolved externals 3512-lab8 c:\users\tess\documents\visual studio 2015\Projects\3512-lab8\Debug\3512-lab8.exe 1
Here are my files
Name.h
#include <string>
#ifndef NAME_H
#define NAME_H
class Name {
public:
Name() {}
Name(const std::string& first, const std::string& last) :first_(toLowerCase(first)), last_(toLowerCase(last)) {}
std::string toLowerCase(const std::string& s);
friend std::istream& operator>>(std::istream& is, Name& n);
friend std::ostream& operator<<(std::ostream& os, const Name& n);
friend bool isValidName(const Name& n);
private:
std::string first_;
std::string last_;
static bool isValidName(const Name& n);
};
#endif
Student.h
#include "Name.h"
#include <string>
#ifndef STUDENT_H
#define STUDENT_H
class Student {
public:
Student(){}
explicit Student(const Name& name, const std::string& id = "A11111111") :id_(id), name_(name) {
if (!isValidId(id_)) {
throw "invalid id";
}
else if (!isValidName(name_)) {
throw "invalid name";
}
}
virtual ~Student(){}
friend std::ostream& operator<<(std::ostream& os, const Student& s);
friend std::istream& operator>>(std::istream& is, Student& s);
friend bool operator<(const Student& lhs, const Student& rhs);
private:
std::string id_;
Name name_;
static bool isValidId(const std::string& id);
};
#endif
Name.cpp
#include "Name.h"
std::istream& operator>>(std::istream& is, Name& n) {
return is >> n.first_ >> n.last_;
}
std::ostream& operator<<(std::ostream& os, const Name& n) {
return os << n.first_ << " " << n.last_;
}
std::string Name::toLowerCase(const std::string& s) {
std::string str = s;
for (std::string::size_type i = 0; i < s.size(); i++) {
tolower(str[i]);
}
return str;
}
bool Name::isValidName(const Name & n){
std::string::size_type i;
std::string::size_type first_size = n.first_.size();
std::string::size_type last_size = n.last_.size();
if (first_size == 0 || last_size == 0) {
return false;
}
for (i = 0; i < first_size; ++i) {
if (!isalpha(n.first_[i])) {
return false;
}
}
for (i = 0; i < last_size; ++i) {
if (!isalpha(n.last_[i])) {
return false;
}
}
return true;
}
Student.cpp
#include "Student.h"
#define SID_LENGTH 9
bool operator<(const Student& lhs, const Student& rhs) {
return lhs.id_ < rhs.id_;
}
bool Student::isValidId(const std::string & id){
std::string::size_type i = 0;
if (id.length() == SID_LENGTH) {
if (id[i] == 'A' || id[i] == 'a') {
for (i = 1; i < id.size(); i++) {
if (!isdigit(id[i])) {
return false;
}
}
return true;
}
}
return false;
}
Main.cpp
#include "Student.h"
#include <iostream>
#include <map>
int main() {
std::map<Student, int> m;
Student s;
int score;
while (std::cin >> s >> score) {
m[s] += score;
}
for (const auto& it : m) {
std::cout << it.first << "" << it.second << std::endl;
}
}
#include .h files in your main() file and NOT the .cpp files
You have declared an overloaded operator<< and operator>> for the Student class, but you never defined them in the .cpp file. I think this is the error you're seeing.
I'm not sure what it looked like before you edited it, but you do need to be including <iostream> in your Name.h file (as well as your Main.cpp file)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#ifndef ViasLigacaoAuto_
#define ViasLigacaoAuto_
#include <iostream>
#include <string>
using namespace std;
#include "LocInteresse.h"
#include "ViasLigacao.h"
class ViasLigacaoAuto : public ViasLigacao
{
private:
float precoportagem;
public:
ViasLigacaoAuto();
ViasLigacaoAuto(LocInteresse* &locInteresse1,LocInteresse* &locInteresse2,string codigodavia,int totalkms,int tempomedio,float precoportagem);
ViasLigacaoAuto(string codigodavia,int totalkms,int tempomedio,float precoportagem);
ViasLigacaoAuto(const ViasLigacaoAuto& va);
~ViasLigacaoAuto();
float getPrecoportagem()const;
void setPrecoportagem(float precoportagem);
void listar()const;
ViasLigacao* ViasLigacaoAuto :: clone() { return new ViasLigacaoAuto(*this);} //criar uma copia de um objecto que ja existe
ViasLigacaoAuto& operator=(const ViasLigacaoAuto &va);
virtual void ViasLigacaoAuto:: escrever(ostream &out) const;
};
//constructores
ViasLigacaoAuto :: ViasLigacaoAuto() : ViasLigacao()
{
this->precoportagem=-1;
}
ViasLigacaoAuto :: ViasLigacaoAuto(LocInteresse* &locInteresse1,LocInteresse* &locInteresse2,string codigodavia,int totalkms,int tempomedio,float precoportagem) :
ViasLigacao(locInteresse1,locInteresse2,codigodavia,totalkms,tempomedio)
{
(*this).setPrecoportagem(precoportagem);
}
ViasLigacaoAuto::ViasLigacaoAuto(const ViasLigacaoAuto& va) : ViasLigacao(va)
{
this->precoportagem=va.precoportagem;
}
ViasLigacaoAuto :: ~ViasLigacaoAuto()
{
}
void ViasLigacaoAuto::setPrecoportagem(float precoportagem)
{
(*this).precoportagem=precoportagem;
}
float ViasLigacaoAuto::getPrecoportagem()const{
return (*this).precoportagem;
}
void ViasLigacaoAuto :: listar() const
{
}
ViasLigacaoAuto& ViasLigacaoAuto::operator=(const ViasLigacaoAuto &va)
{
if(this!=&va)
{
precoportagem=va.precoportagem;
ViasLigacao::operator=(va);
}
return (*this);
}
void ViasLigacaoAuto :: escrever(ostream &out) const
{
out << "Autoestrada " << endl;
ViasLigacao::escrever(out); //atributos herdados da classe viasligacao
out << "Preco da portagem: " << precoportagem << endl;
}
ostream& operator<<(ostream &o, const ViasLigacaoAuto &va)
{
va.escrever(o);
return o;
}
#endif
*
#pragma once
#ifndef _ApViasLigacao_
#define _ApViasLigacao_
#include <ostream>
using namespace std;
#include "ViasLigacao.h"
#include "ViasLigacaoAuto.h"
#include "ViasLigacaoNacional.h"
#include "Teste.h"
#include "LocInteresse.h"
#include "LocInteresseCult.h"
#include "LocInteresseNat.h"
//---------Classe apontador para ViasLigacao---------
class ApViasLigacao {
private:
ViasLigacao *apvl;
enum TipoComparacao { KMS , CUSTO ,TEMPO};
static TipoComparacao tipoComparacao;
public:
static void setComparacaoKMS();
static void setComparacaoCUSTO();
static void setComparacaoTEMPO();
int getTotalkms() const;
virtual float getPrecoportagem() const;
int getTempomedio()const;
LocInteresse* getLocIni();
void setLocIni(LocInteresse);
LocInteresse* getLocFim();
void setLocFim(LocInteresse);
ApViasLigacao();
ApViasLigacao(ViasLigacao* &vl);
ApViasLigacao(string codigodavia,int totalkms,int tempomedio,float precoportagem);
ApViasLigacao(string codigodavia,int totalkms,int tempomedio,string pavimento);
ApViasLigacao(const ApViasLigacao &vl);
~ApViasLigacao();
const ApViasLigacao & operator=(const ApViasLigacao &vl);
bool operator >(const ApViasLigacao &vl) const;
bool operator <(const ApViasLigacao &vl) const;
bool operator ==(const ApViasLigacao &vl) const;
ApViasLigacao operator+(const ApViasLigacao &vl);
const ApViasLigacao & operator+=(const ApViasLigacao &vl);
void write(ostream &out) const;
};
ApViasLigacao::TipoComparacao ApViasLigacao::tipoComparacao=ApViasLigacao::TipoComparacao::KMS;
void ApViasLigacao::setComparacaoKMS() {
tipoComparacao=TipoComparacao::KMS;
}
void ApViasLigacao::setComparacaoCUSTO() {
tipoComparacao=TipoComparacao::CUSTO;
}
void ApViasLigacao::setComparacaoTEMPO(){
tipoComparacao=TipoComparacao::TEMPO;
}
int ApViasLigacao::getTotalkms() const {
return apvl->getTotalkms();
}
float ApViasLigacao::getPrecoportagem() const {
return apvl->getPrecoportagem();
}
int ApViasLigacao::getTempomedio() const {
return apvl->getTempomedio();
}
ApViasLigacao::ApViasLigacao() {
this->apvl = new ViasLigacaoAuto();
}
ApViasLigacao::ApViasLigacao(string codigodavia,int totalkms,int tempomedio,float precoportagem) {
apvl = new ViasLigacaoAuto(codigodavia, totalkms,tempomedio, precoportagem);
}
ApViasLigacao::ApViasLigacao(string codigodavia,int totalkms,int tempomedio,string pavimento) {
apvl = new ViasLigacaoNacional(codigodavia,totalkms,tempomedio,pavimento);
}
ApViasLigacao::ApViasLigacao(ViasLigacao* &vl) {
this->apvl = vl->clone();
}
ApViasLigacao::ApViasLigacao(const ApViasLigacao &vl) {
this->apvl = vl.apvl->clone();
}
ApViasLigacao::~ApViasLigacao() {
delete apvl;
}
//bool compara(int km1, int km2, double c1, double c2, std::
bool ApViasLigacao::operator >(const ApViasLigacao &vl) const {
if (tipoComparacao==TipoComparacao::KMS) return (*this).getTotalkms() > vl.getTotalkms();
if (tipoComparacao==TipoComparacao::CUSTO){
if((*this).getPrecoportagem() == vl.getPrecoportagem()){
return (*this).getTotalkms() > vl.getTotalkms();
}
return (*this).getPrecoportagem() > vl.getPrecoportagem();
}
return (*this).getTempomedio() > vl.getTempomedio();
}
bool ApViasLigacao::operator <(const ApViasLigacao &vl) const {
if (tipoComparacao==TipoComparacao::KMS) return (*this).getTotalkms() < vl.getTotalkms();
if (tipoComparacao==TipoComparacao::CUSTO){
if((*this).getPrecoportagem() == vl.getPrecoportagem()){
return (*this).getTotalkms() < vl.getTotalkms();
}
return (*this).getPrecoportagem() < vl.getPrecoportagem();
}
return (*this).getTempomedio() < vl.getTempomedio();
}
bool ApViasLigacao::operator ==(const ApViasLigacao &vl) const {
if (tipoComparacao==TipoComparacao::KMS) return (*this).getTotalkms() == vl.getTotalkms();
if (tipoComparacao==TipoComparacao::CUSTO) return (*this).getPrecoportagem() == vl.getPrecoportagem();
return (*this).getTempomedio() == vl.getTempomedio();
}
ApViasLigacao ApViasLigacao::operator+(const ApViasLigacao &vl) {
return ApViasLigacao("", (*this).getTotalkms()+vl.getTotalkms(), (*this).getTempomedio()+vl.getTempomedio(), (*this).getPrecoportagem()+vl.getPrecoportagem());
}
const ApViasLigacao & ApViasLigacao::operator+=(const ApViasLigacao &vl) {
this->apvl->setTotalkms(this->apvl->getTotalkms()+vl.apvl->getTotalkms());
this->apvl->setTempomedio(this->apvl->getTempomedio()+vl.apvl->getTempomedio());
if (typeid(*apvl)==typeid(ViasLigacaoAuto)) {
ViasLigacaoAuto *vla = (ViasLigacaoAuto *)this->apvl;
vla->setPrecoportagem(vla->getPrecoportagem()+vl.apvl->getPrecoportagem());
}
return *this;
}
const ApViasLigacao & ApViasLigacao::operator=(const ApViasLigacao &vl) {
this->apvl = vl.apvl->clone();
return *this;
}
void ApViasLigacao::write(ostream &out) const {
out << *apvl;
}
ostream &operator <<(ostream &out, const ApViasLigacao &vl)
{
vl.write(out);
return out;
}
#endif
*
#define _CRT_SECURE_NO_WARNINGS
#ifndef ViasLigacao_
#define ViasLigacao_
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "LocInteresse.h"
class ViasLigacao
{
private:
LocInteresse locInteresse1, locInteresse2;
string codigodavia;
int totalkms;
int tempomedio;
public:
ViasLigacao();
ViasLigacao(LocInteresse* locInteresse1,LocInteresse* locInteresse2,string codigodavia,int kms,int tempo);
ViasLigacao(const ViasLigacao &v);
virtual ~ViasLigacao(); //destructor
void listar()const;
virtual ViasLigacao* clone()const;
LocInteresse* getLocInteresse1();
void setLocInteresse1(LocInteresse);
LocInteresse* getLocInteresse2();
void setLocInteresse2(LocInteresse);
string getCodigodavia()const;
void setCodigodavia(string codigodavia);
int getTotalkms()const;
void setTotalkms(int kms);
int getTempomedio()const;
void setTempomedio(int tempo);
virtual float getPrecoportagem() const;
const ViasLigacao & operator=(const ViasLigacao &v);
bool operator>(const ViasLigacao &v) const;
bool operator==(const ViasLigacao &v) const;
bool operator <(const ViasLigacao &v) const;
bool operator<=(const ViasLigacao &v)const;
ViasLigacao operator+(const ViasLigacao &v);
const ViasLigacao & operator+=(const ViasLigacao &v);
void escrever(ostream &out) const; // só se mete VIRTUAL quando sao subclasses
};
ViasLigacao::ViasLigacao()
{
codigodavia = " ";
totalkms= -1;
tempomedio=-1;
}
ViasLigacao::ViasLigacao(LocInteresse* LocInteresse1, LocInteresse* LocInteresse2,string codigodavia, int totalkms,int tempomedio)
{
//this->locInteresse1=LocInteresse1;
// this->locInteresse2=LocInteresse2;
this->codigodavia = codigodavia;
this->totalkms = totalkms;
this->tempomedio=tempomedio;
}
ViasLigacao::ViasLigacao(const ViasLigacao & v)
{
this->locInteresse1=v.locInteresse1;
this->locInteresse2=v.locInteresse2;
this->codigodavia = v.codigodavia;
this->totalkms = v.totalkms;
this->tempomedio= v.tempomedio;
}
ViasLigacao::~ViasLigacao()
{
}
//set's e get's
void ViasLigacao::setLocInteresse1(LocInteresse locInteresse1)
{
locInteresse1 = locInteresse1;
}
LocInteresse* ViasLigacao::getLocInteresse1(){
return &locInteresse1;
}
void ViasLigacao::setLocInteresse2(LocInteresse locInteresse2)
{
locInteresse2 = locInteresse2;
}
LocInteresse* ViasLigacao::getLocInteresse2(){
return &locInteresse2;
}
void ViasLigacao::setCodigodavia(string codigodavia)
{
this->codigodavia = codigodavia;
}
string ViasLigacao::getCodigodavia()const{
return this->codigodavia;
}
void ViasLigacao::setTotalkms(int totalkms)
{
this->totalkms = totalkms;
}
int ViasLigacao::getTotalkms()const
{
return this->totalkms;
}
void ViasLigacao::setTempomedio(int tempomedio)
{
this->tempomedio = tempomedio;
}
int ViasLigacao::getTempomedio()const
{
return this->tempomedio;
}
ViasLigacao* ViasLigacao ::clone() const{
return new ViasLigacao(*this);
}
float ViasLigacao::getPrecoportagem() const{
return 0;
}
bool ViasLigacao :: operator>(const ViasLigacao &v)const{
if(totalkms > v.totalkms)
{
return true;
}
return false;
}
bool ViasLigacao :: operator==(const ViasLigacao &v)const{
if(totalkms > v.totalkms)
{
return true;
}
return false;
}
const ViasLigacao& ViasLigacao:: operator= (const ViasLigacao& v){
if(&v != this){
(*this)=ViasLigacao(v);
}
return *this;
}
ostream& operator<<(ostream &o, const ViasLigacao &v)
{
v.escrever(o);
return o;
}
void ViasLigacao :: escrever(ostream &out) const
{
out << "Local de Interesse inicial:" << locInteresse1 << endl;
out << "Local de Interesse final:" << locInteresse2 << endl;
out << "Via de ligação: " << endl;
out << "Codigo da via: " << codigodavia << endl;
out << "Total de kms: " << totalkms << endl;
out << "Tempo medio: " << tempomedio << endl;
}
#endif
can anyone help?
i get this error
Error 6 error LNK2019: unresolved external symbol "public: __thiscall ViasLigacaoAuto::ViasLigacaoAuto(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int,float)" (??0ViasLigacaoAuto##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##HHM#Z) referenced in function "public: __thiscall ApViasLigacao::ApViasLigacao(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int,float)" (??0ApViasLigacao##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##HHM#Z) C:\Users\joaopedro\Source\Repos\1110830_1121107_esinf\MapaDigital\Main.obj MapaDigital
It's saying it can't find the third constructor you've declared for ViasLigacaoAuto (the one that takes string, int, int, float) - and indeed, that constructor doesn't seem to be defined in your code. You need to define it.
(Just occasionally the compiler/linker error messages tell you what the problem actually is.)
in my school assignment i need a small help
this is my header file:
#include <iostream>
#include <cstring>
using namespace std;
#include "ISBNPrefix.h"
class ISBN
{
char str[11];
char area[6];
char publisher[8];
char title[7];
bool registered;
public:
ISBN();
ISBN(const char*,ISBNPrefix &);
void toStr(char*)const;
void toStrWithStyle(char*)const;
bool empty()const;
bool isRegistered() const;
bool read(istream& is, const ISBNPrefix& list);
void display(ostream&) const;
};
int isValid(const char* str);
and this is the implementation of my file:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <iomanip>
using namespace std;
#include "ISBN.h"
ISBN::ISBN()
{
str[0]='\0';
area[0]='\0';
publisher[0]='\0';
title[0]='\0';
registered=false;
}
ISBN::ISBN(const char* s,ISBNPrefix& p)
{
if(isValid(s)==1)
{
strcpy_s(str,s);
}
else
{
*this=ISBN();
}
}
bool ISBN::empty()const
{
bool chk=false;
if(str[0]=='\0')
chk=true;
return chk;
}
void ISBN::toStrWithStyle(char* s) const
{
if(registered)
{
sprintf(s,"%s-%s-%s-%c",area,publisher,title,str[9]);
}
else
{
toStr(s);
}
}
void ISBN::toStr(char* s) const
{
if (str[0]!='\0')
strcpy(s,str);
else
strcpy(s,"no data");
}
void ISBN::display(ostream & os) const
{
char str[14];
toStrWithStyle(str);
cout<< setw (13) <<str;
}
int isValid(const char* str)
{
int rc=0;
if(str!=0)
{
int sum,i=0;
sum=0;
for(i=0;i<10;i++)
sum+=(str[i]-'0')*(10-i);
if(sum%11==0)
{
rc= 1;
}
}
else
rc=0;
return rc;
}
bool ISBN::read(istream& is, const ISBNPrefix& list)
{
char str[11];
bool quit=false;
bool ok=false;
char lists;
do{
cout<<"ISBN (0 to quit) : ";
is.getline(str,11); //or is.get(str,11)
if(strcmp(str,"0")==0)
quit=true;
else if (isValid(str)==1)
{
*this=ISBN(str,list);
ok=true;
cout<<"isbn is valid"<<endl;
}
else
{
*this=ISBN();
cout<<"invalid ISBN"<<endl;
}
} while(!quit&&!ok);
return !quit;
}
in the ISBN::read where I say
*this=ISBN(str,list);
i want to overload another member but i can't.
can anyone tell me how can i do that?
First I would suggest use std::string in favour of char[]. It will save a lot of trouble. For reading ISBN I would write something like this:
bool ISBN::read(istream& is)
{
ISBN result;
// reading into result
std::swap(*this,result);
return !quit;
}
Or even better (as a non member function):
std::istream& operator>>(istream& is, ISBN& obj)
{
ISBN result;
// reading into result
is(!quit)
is.clear(std::ios_base::failbit);
std::swap(obj,result);
return is;
}
In any way you should RAII classes for your resources. In your special case std::string instead of char[].