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)
Related
I am working on my school project that has a base class and a derived class with some other classes.
Product.h
#ifndef AMA_PRODUCT_H
#define AMA_PRODUCT_H
#include <iostream>
#include <fstream>
namespace AMA
{
const int max_sku_length = 7;
const int max_unit_length = 10;
const int max_name_length = 75;
const double TRate = 0.13;
class Product
{
private:
char m_type;
char m_sku[max_sku_length +1];
char m_unit[max_unit_length + 1];
char* m_name;
int m_Cquantity;
int m_Nquantity;
double m_price;
bool m_status;
protected:
void sku(const char* setSku) { strncpy(m_sku, setSku, max_sku_length); }
const char* name() const { return m_name; }
bool taxed() const { return m_status; }
const char* sku() const { return m_sku;}
void name(const char*);
const char* unit() const;
double price() const;
void message(const char*);
bool isClear() const;
public:
double cost() const;
bool operator==(const char* src) { return strcmp(m_sku, src) == 0; }
bool isEmpty() const { return ((m_sku[0] == '\0') && (m_name == nullptr) && (m_price == 0) && (m_Cquantity == 0)); }
int qtyNeeded() const { return m_Nquantity; }
int quantity() const { return m_Cquantity; }
int operator+=(int src) { return m_Cquantity += src; }
Product();
Product(const char* sku, const char* name, const char* unit, int qty = 0,
bool taxed = true, double price = 0.0, int qtyNeeded = 0);
Product(const Product&);
Product& operator=(const Product&);
~Product();
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
double total_cost() const;
void quantity(int);
bool operator>(const char*) const;
bool operator>(const Product&) const;
};
std::ostream& operator<<(std::ostream&, const Product&);
std::istream& operator>>(std::istream&, Product&);
double operator+=(double&, const Product&);
}
#endif
MyProduct.h
#ifndef AMA_MY_PRODUCT_H
#define AMA_MY_PRODUCT_H
#include <fstream>
#include "Product.h"
#include "ErrorState.h"
namespace AMA {
class MyProduct : public Product {
public:
MyProduct();
MyProduct(const char* sku, const char* name, const char* unit, int qty = 0,
bool isTaxed = true, double price = 0.0, int qtyNeeded = 0);
const char* sku() const;
const char* name() const;
const char* unit() const;
bool taxed() const;
double price() const;
double cost() const;
};
class Test {
MyProduct product; // Error
const char* filename;
public:
Test(const char* file);
Test(const char* file, const char* theSku, const char* theName);
std::fstream& store(std::fstream& file, bool addNewLine = true) const;
std::fstream& load(std::fstream& file);
std::ostream& write(std::ostream& os, bool linear) const;
std::istream& read(std::istream& is);
int operator+=(int value);
bool operator==(const char* sku) const;
friend std::ostream& operator<<(std::ostream& os, const Test& test);
friend double operator+=(double& d, const Test& test);
friend std::istream& operator>>(std::istream& is, Test& test);
};
}
#endif
MyProduct.cpp
#include <iomanip>
#include <fstream>
#include <cstring>
#include "MyProduct.h"
#ifdef TAB
#undef TAB
#endif
#define TAB '\t'
using namespace std;
namespace AMA {
MyProduct::MyProduct() : Product("", "", "") {}
MyProduct::MyProduct(const char* sku, const char* name, const char* unit, int qty,
bool isTaxed, double price, int qtyNeeded) :
Product(sku, name, unit, qty, isTaxed, price, qtyNeeded) {}
const char* MyProduct::sku() const { return Product::sku(); }
const char* MyProduct::name() const { return Product::name(); }
const char* MyProduct::unit() const { return Product::unit(); }
bool MyProduct::taxed() const { return Product::taxed(); }
double MyProduct::price() const { return Product::price(); }
double MyProduct::cost() const { return Product::cost(); }
Test::Test(const char* file) : filename(file) { }
Test::Test(const char* file, const char* theSku, const char* theName) :
product(theSku, theName, ""), filename(file) { }
std::fstream& Test::store(std::fstream& file, bool addNewLine) const {
if (!product.isEmpty()) {
file.open(filename, ios::out | ios::app);
file << product.sku() << TAB << product.name() << TAB << product.unit() << TAB <<
(product.taxed() ? 1 : 0) << TAB << product.price() << TAB << product.quantity() << TAB <<
product.qtyNeeded() << endl;
file.clear();
file.close();
}
return file;
}
std::fstream& Test::load(std::fstream& file) {
char sku_[max_sku_length + 1];
char name[max_name_length + 1];
char unit[max_unit_length + 1];
int quantity, qtyNeeded;
double price_;
char taxed_;
file.open(filename, ios::in);
file >> sku_;
file >> name;
file >> unit;
file >> taxed_;
file >> price_;
file >> quantity;
file >> qtyNeeded;
file.clear();
file.close();
product = MyProduct(sku_, name, unit, quantity, taxed_ != 0, price_, qtyNeeded); //ERROR
return file;
}
std::ostream& Test::write(std::ostream& os, bool linear) const {
return product.isEmpty() ? os : (os << product.sku() << ": " << product.name() << ", quantity: "
<< product.quantity() << ", quantity needed:" << product.qtyNeeded()
<< ", Cost: " << fixed << setprecision(2) << product.cost());
}
std::istream& Test::read(std::istream& is) {
char sku_[max_sku_length + 1];
char name[max_name_length + 1];
char unit[max_unit_length + 1];
int quantity, qtyNeeded;
double price_;
char taxed_;
cout << " Sku: ";
is >> sku_;
cout << " Name (no spaces): ";
is >> name;
cout << " Unit: ";
is >> unit;
cout << " Taxed? (y/n): ";
is >> taxed_;
cout << " Price: ";
is >> price_;
cout << " Quantity On hand: ";
is >> quantity;
cout << " Quantity Needed: ";
is >> qtyNeeded;
product = MyProduct(sku_, name, unit, quantity, taxed_ != 0, price_, qtyNeeded); //ERROR
return is;
}
int Test::operator+=(int value) {
product.quantity(product += value);
return product.quantity();
}
bool Test::operator==(const char* sku) const {
return !std::strcmp(product.sku(), sku);
}
std::ostream& operator<<(std::ostream& os, const Test& test) {
return test.product.write(os, true);
}
double operator+=(double& d, const Test& test) {
return d += test.product.total_cost();
}
std::istream& operator>>(std::istream& is, Test& test) {
return test.product.read(is);
}
}
I am getting errors that I don't really know how to fix
object of abstract class type "AMA::MyProduct" is not allowed
'AMA::MyProduct': cannot instantiate abstract class
a cast to abstract class "AMA::MyProduct" is not allowed
Can someone help me out please?
The errors are self explanatory.
AMA::MyProduct is an abstract class, and such it cannot be instantiated directly.
A class is abstract when it has at least 1 abstract method (virtual and = 0 keywords on it). An abstract method MUST be overridden in a derived class, and you MUST instantiate that derived class, not the abstract class.
AMA::MyProduct is abstract because it does not override the 4 abstract methods it inherits from AMA::Product:
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
You implemented them in AMA::Test instead of in AMA::MyProduct.
I've been pulling my hair out over a certain error that seems to be plaguing my program. I've attempted to search online for cases similar to mine but I can't seem to find a way to apply the other solutions to this problem. My issue is as follows: When I initially open the program it immediately stops responding and crashes. Debugging led me to find the error in question is "0xC0000005: Access violation writing location 0xCCCCCCCC". It seems to be tied to me assigning two attributes (sku_ and name_ ) as '\0'. If I change these values to anything else such as "" or even "\0" the program runs in visual studio, but will fail to compile elsewhere. Could someone help me understand where I am going wrong?
Product.h
#ifndef SICT_Product_H__
#define SICT_Product_H__
#include "general.h"
#include "Streamable.h"
#include <cstring>
namespace sict {
class Product : public Streamable {
char sku_ [MAX_SKU_LEN + 1];
char* name_;
double price_;
bool taxed_;
int quantity_;
int qtyNeeded_;
public:
//Constructors
Product();
Product(const char* sku, const char* name1, bool taxed = true, double price = 0, int qtyNeeded =0);
Product(Product& g);
~Product();
//Putter Functions
void sku(const char* sku) { strcpy(sku_,sku); };
void price(double price) {price_ = price;};
void name(const char* name);
void taxed(bool taxed) { taxed_ = taxed; };
void quantity(int quantity) { quantity_ = quantity; };
void qtyNeeded(int qtyNeeded) { qtyNeeded_ = qtyNeeded; };
//Getter functions
const char* sku() const { return sku_; };
double price() const { return price_; };
const char* name() const { return name_; };
bool taxed() const { return taxed_; };
int quantity() const { return quantity_; };
int qtyNeeded() const { return qtyNeeded_; };
double cost() const;
bool isEmpty() const;
Product& operator=(const Product& );
bool operator==(const char* );
int operator+=(int );
int operator-=(int );
};
double operator+=(double& , const Product& );
std::ostream& operator<<(std::ostream& os, const Product& );
std::istream& operator>>(std::istream& is, Product& );
}
#endif
Product.cpp
#include <iostream>
#include <cstring>
#include "Product.h"
namespace sict {
Product::Product() {
sku_[0] = '\0';
name_[0] = '\0';
price_ = 0;
quantity_ = 0;
qtyNeeded_ = 0;
}
Product::Product(const char* sku, const char* name1, bool taxed1, double price1, int qtyNeeded1) {
strncpy(sku_, sku, MAX_SKU_LEN);
name(name1);
quantity_ = 0;
taxed(taxed1);
price(price1);
qtyNeeded(qtyNeeded1);
}
double Product::cost() const {
if (taxed_ == true) {
return (price_ * TAX) + price_;
}
else
return price_;
}
bool Product::isEmpty() const{
if (sku_ == nullptr && name_ == nullptr && quantity_ == 0 && price_ == 0 && qtyNeeded_ == 0) {
return true;
}
else
return false;
}
Product::Product(Product& ex) {
sku(ex.sku_);
price(ex.price_);
name(ex.name_);
taxed(ex.taxed_);
quantity(ex.quantity_);
qtyNeeded(ex.qtyNeeded_);
}
Product& Product::operator=(const Product& g) {
sku(g.sku_);
price(g.price_);
name(g.name_);
taxed(g.taxed_);
quantity(g.quantity_);
qtyNeeded(g.qtyNeeded_);
return *this;
}
Product::~Product() {
delete [] name_;
}
void Product::name(const char* name) {
name_ = new char [strlen(name) + 1];
strcpy(name_, name);
}
bool Product::operator==(const char* right) {
if (sku_ == right) {
return true;
}
else
return false;
}
int Product::operator+=(int g) {
quantity_ = quantity_ + g;
return quantity_;
}
int Product::operator-=(int g) {
quantity_ = quantity_ - g;
return quantity_;
}
double operator+=(double& p, const Product& right) {
p = p + (right.cost() * right.quantity());
return p;
}
std::ostream& operator<<(std::ostream& os, const Product& g) {
return g.write(os, true);
}
std::istream& operator>>(std::istream& is, Product& g) {
return g.read(is);
}
}
The General.h file referenced in the header is just a list of constant values such as the "TAX" and "MAX_SKU_LEN" values.
The Streamable header contains pure virtual functions. I will list it here in case it is needed.
Streamable.h
#ifndef SICT__Streamable_H_
#define SICT__Streamable_H_
#include <iostream>
#include <fstream>
#include "Product.h"
namespace sict {
class Streamable {
public:
virtual std::fstream& store(std::fstream& file, bool addNewLine = true)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
};
}
#endif
Thank you very much in advance.
Product::Product() {
sku_[0] = '\0';
name_[0] = '\0'; // <---- writing via unitialized pointer
price_ = 0;
quantity_ = 0;
qtyNeeded_ = 0;
}
There's one possible source of your problems. You have defined a char pointer (a dynamic array or a C-string, that is) name_ in your class but you never allocate any memory for it in your constructor, before attempting to record a value via the pointer. Naturally, you get a write access violation.
Before assigning the value to char at index [0] you need to first allocate space for at least one element in your string, e.g. by doing name_ = new char [1]. Alternatively, you may choose to initialize the pointer itself to NULL (or nullptr) and use that to indicate that name_ has not yet been set.
I have this class:
#ifndef String_H
#define String_H
class String
{
public:
String();
String(const char*, ...);
String(const String&);
~String();
const String& operator= (const char*);
const int capacity();
void clear(){ string[0] = '\0'; }
const char* getString()const { return string; }
const int lenght()const { return length; }
private:
int length;
char *string;
void alloc(const int);
};
#endif
And in the implementation i have this:
#include <wtypes.h>
#include "String.h"
#include "Log.h"
#include <stdio.h>
String::String()
{
alloc(1);
clear();
}
String::String(const char* _string, ...)
{
//length = 0;
if (_string != NULL)
{
static char buff1[4096];
va_list args;
va_start(args, _string);
int res = vsprintf_s(buff1, 4096, _string, args);
va_end(args);
if (res > 0)
{
alloc(res + 1);
strcpy_s(string, length, buff1);
}
}
else
{
alloc(1);
clear();
}
}
String::String(const String& _string)
{
if (&_string != NULL)
{
alloc(_string.lenght());
strcpy_s(string, length, _string.getString());
}
else
{
alloc(1);
clear();
}
}
String::~String()
{
delete[]string;
}
const String& String::operator= (const char* str)
{
if (strlen(str) > sizeof(string) + 1)
{
delete[] str;
alloc(strlen(str) + 1);
strcpy_s(string, length, str);
}
else
{
strcpy_s(string, length, str);
}
return (*this);
}
void String::alloc(const int size)
{
length = size;
string = new char[size];
}
And when in main I do:
String a;
String b("hi");
a = b;
The compiler says me that:
Error 2 error LNK2019: unresolved external symbol "public: class String const & __thiscall String::operator=(class String const &)" (??4String##QAEABV0#ABV0##Z) referenced in function _main C:..\main.obj
AND
Error 3 error LNK1120: 1 unresolved externals C:..\MyLibrary.exe
This is making me crazy. Please help me.
I can't see what i'm doing wrong.
This line invokes the assignment operator:
a = b;
You are missing an assignment operator that takes a String.
This is not the assignment operator that will be called:
const String& String::operator= (const char* str)
A typical assignment operator would have the following signature:
String& String::operator= (const String& s)
Please read up on the "Rule of 3". What is The Rule of Three?
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.)
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;