Unresolved Externals between functions - c++

I'm getting an unresolved external error but I can't figure out exactly what's causing it.
error LNK2019: unresolved external symbol "public: __thiscall ABC::ABC(class ABC const &)" (??0ABC##QAE#ABV0##Z) referenced in function "public: __thiscall hasDMA::hasDMA(class hasDMA const &)" (??0hasDMA##QAE#ABV0##Z)
1>C:\Users\Matt\documents\visual studio 2010\Projects\GSP_125_Lab5\Debug\GSP_125_Lab5.exe : fatal error LNK1120: 1 unresolved externals
The program runs when I delete this block of code:
hasDMA::hasDMA(const hasDMA & hs) : ABC(hs)
{
style = new char[std::strlen(hs.style) + 1];
std::strcpy(style, hs.style);
}
But I don't know what part of that is being referenced elsewhere.
Here is my ABC header and hasDMA header.
class ABC
{
private:
enum {MAX = 35};
char label[MAX];
int rating;
protected:
const char * Label() const {return label;}
int Rating() const {return rating;}
public:
ABC(const char * l = "null", int r = 0);
ABC(const ABC & rs);
virtual ~ABC() {};
virtual ABC & operator*() { return *this; }
ABC & operator=(const ABC & rs);
virtual void View() const = 0;
friend std::ostream & operator<<(std::ostream & os, const ABC & rs);
};
class hasDMA :public ABC
{
private:
char * style;
public:
hasDMA(const char * s = "none", const char * l = "null",
int r = 0);
hasDMA(const char * s, const ABC & rs);
hasDMA(const hasDMA & hs);
~hasDMA(){};
hasDMA & operator=(const hasDMA & rs);
virtual void View() const;
};
These are the only two ABC methods I have:
ABC::ABC(const char *l, int r)
{
std::strcpy(label, l);
label[MAX - 1] = '\0';
rating = r;
}
ABC & ABC::operator=(const ABC & rs)
{
if (this == &rs)
return *this;
strcpy(label, rs.label);
return *this;
}
If it helps these are the rest of my hasDMA methods:
hasDMA::hasDMA(const char *s, const char *l, int r) : ABC (l, r)
{
std::strcpy(style, s);
}
hasDMA::hasDMA(const char *s, const ABC & rs)
{
std::strcpy(style, s);
}
void hasDMA::View() const
{
cout << "Record Label: " << Label() << endl;
cout << "Rating: " << Rating() << endl;
cout << "Style: " << style << endl;
}
hasDMA & hasDMA::operator=(const hasDMA & hs)
{
if (this == &hs)
return *this;
ABC::operator=(hs);
style = new char[std::strlen(hs.style) +1];
std::strcpy(style, hs.style);
return *this;
}

You have declared an ABC copy constructor, but you haven't defined it.
ABC(const ABC & rs); // declaration.
You will need to provide a definition, or remove the declaration.
You could greatly simplify the class by using an std::string instead of the char array. You wouldn't need to provide an assignment operator, copy constructor or destructor.

You forgot to define implementation for ABC(const ABC & rs);

Related

Is this approach change the object of the name only?

I want Swap the name (i.e. string in cName[]) of the two cats using the pointer approach.
However I want to Only swap the name, NOT the object.
Am I correct?
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string.h>
using namespace std;
class CAT
{
public:
CAT(char * firstname) { strncpy(cName, firstname, 79); }
~CAT() { ; }
char * getName() { return cName; }
void setName(char *nameinput) { strncpy(cName, nameinput, 79); }
private:
char cName[80];
};
void nameSwap(CAT *CatA, CAT *CatB)
{
char testing[] = "testing";
CAT temp =CAT(testing);
temp = *CatA;
*CatA = *CatB;
*CatB = temp;
}
int main()
{
char Taby[] = "Taby";
char Felix[] = "Felix";
CAT pA = CAT(Taby);
CAT pB = CAT(Felix);
cout << "The inital name pA is " << pA.getName() << " and pA is" << pB.getName() << endl;
nameSwap(&pA, &pB);
cout << "After approach" << endl;
cout << "The name pA is " << pA.getName() << " and " << pB.getName() << endl;
system("PAUSE");
return 0;
}
You are actually swapping the whole objects, not only the name of the CAT.
If you only want to swap the name, you need to access the cName member in a similar way as you are doing for the objects. You'd also need permission to access to the cName member in such a swap function, which a function outside won't have since cName is private. Make the swap function a member of your class:
class CAT
{
public:
CAT(const char* firstname) { strncpy(cName, firstname, 80); }
~CAT() {}
const char* getName() const { return cName; }
void setName(const char *nameinput) { strncpy(cName, nameinput, 80); }
void swapName(CAT& CatB)
{
char tmp[80];
strncpy(tmp, CatB.cName, 80);
strncpy(CatB.cName, cName, 80);
strncpy(cName, tmp, 80);
}
private:
char cName[80];
// other CAT attributes won't be affected by the name swap
};
And call it like this
pA.swapName(pB); // or pB.swapName(pA); - same result
But consider using std::string instead of char[]. You'll soon find C++ strings much easier to work with and also, when swapping those, only the pointers to the underlying memory is swapped, so it's more effective.
std::string one;
std::string two;
one.swap(two);
Edit: As per request, I added a version using pointers.
I made it in a haste and haven't debugged it so I've probably made a lot of mistakes. First, I made a new class called wong_string that will hold the name and any other attributes suiteable for strings.
#include <stdexcept>
#include <cstring>
class wong_string {
char* m_data;
static char* duplicate(const char* str) {
size_t len = std::strlen(str)+1;
char* rv = new char[len];
std::memcpy(rv, str, len);
return rv;
}
public:
// default constructor: wong_string howdy1;
wong_string() : m_data(nullptr) {}
// conversion constructor: wong_string howdy2("value2");
wong_string(const char* cstr) :
m_data(duplicate(cstr))
{}
// copy constructor: wong_string howdy3 = howdy2;
wong_string(const wong_string& rhs) : wong_string(rhs.m_data) {}
// move constructor: wong_string howdy4 = wong_string("value4");
wong_string(wong_string&& rhs) : m_data(rhs.m_data) {
rhs.m_data = nullptr;
}
// copy assignment operator: (wong_string howdy5;) howdy5 = howdy4;
wong_string& operator=(const wong_string& rhs) {
if(this!=&rhs) {
char* tmp = duplicate(rhs.m_data);
if(m_data) delete []m_data;
m_data = tmp;
}
return *this;
}
// copy assignment operator from c string
wong_string& operator=(const char* rhs) {
*this = wong_string(rhs);
return *this;
}
// move assignment operator: (wong_string howdy6;) howdy6 = wong_string("value6");
wong_string& operator=(wong_string&& rhs) {
if(this!=&rhs) {
m_data = rhs.m_data;
rhs.m_data = nullptr;
}
return *this;
}
// destructor, free memory allocated by duplicate(), if any
~wong_string() {
if(m_data) delete []m_data;
}
// comparisons
bool operator==(const wong_string& rhs) const {
return strcmp(m_data, rhs.m_data)==0;
}
bool operator!=(const wong_string& rhs) const {
return !(*this==rhs);
}
// conversion to a normal c string
operator char const* () const { return m_data; }
// output stream operator
friend std::ostream& operator<<(std::ostream&, const wong_string&);
// input stream operator - not implemented yet
};
with that in place, your CAT can be made into something like this:
class CAT
{
public:
CAT(const char* firstname, const char* nickname=nullptr) :
cName(firstname),
cNickName(nickname?nickname:firstname)
{}
~CAT() {}
const char* getName() const { return cName; }
void setName(const char *nameinput) { cName=nameinput; }
void swapName(CAT& CatB)
{
std::swap(cName, CatB.cName);
}
private:
wong_string cName; // Madame Florence Jenkins III
// other CAT attributes won't be affected by the name swap
wong_string cNickName; // Ms. Miao
};
So, there you have it. Pointers galore...

'AMA::MyProduct': cannot instantiate abstract class

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.

C++ oop multiple linker errors

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)

Error LNK2019 and dont know what is happening

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?

error LNK2019:unresolved external symbol [closed]

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.)