I am just learning how to use C/C++ and am trying to write a program in which "otherPerson" (first name, last name) inherits from "person" (first name). I'm stuck on the compareTo function for person. It is supposed to sort an array of pointers to person alphabetically by first name. (not sure if I expressed that idea correctly). The end goal is to print the sorted contents of the array.
I keep getting:
error LNK2019: unresolved external symbol "void __cdecl compareTo(class person * * const,int)" (?compareTo##YAXQAPAVperson##H#Z) referenced in function _main
1>c:\users\laur\documents\visual studio 2012\Projects\Project1\Debug\Project1.exe : fatal error LNK1120: 1 unresolved externals
when I try to build. I've looked around the internet for more information, but I'm pretty sure my include statements are correct. The function is intended as a member function.
Test.cpp:
#include "stdio.h"
#include "otherPerson.h"
#include<iostream>
#include <string>
using namespace std;
void compareTo(person *array[7], int );
int main(){
int length = 7;
person* epicJourney[6];
//fill array
compareTo(epicJourney, length);
person.h:
#include <stdio.h>
#include <string>
using namespace std;
class person {
protected:
string firstName;
public:
person(string firstName);
virtual void setFirstName(string firstName);
virtual string getFirstName();
virtual void compareTo (person *array[7], int length);
virtual string toString();
};
person.cpp:
#include "person.h"
#include <string>
using namespace std;
person::person(string firstName){
this->firstName = firstName;
}
void person::setFirstName(string aName){
firstName =aName;
}
string person::getFirstName(){
return ((*this).firstName);
}
string person::toString(){
return (this->firstName);
}
void person::compareTo(person *array[], int length){
int i;
int j;
person *current;
for (i=1; i<length; i++){
current = array[i];
for (j=i; j>=1 && (current < array[j-1]); j--){
array[j] = array[j-1];
array[j-1] = current;
}
}
}
You prototype is void compareTo(... ); but your actual implementation is void person::compareTo(...) . You have to make up your mind: do you want a class member function?
You're missing the definition of this standalone function, :
void compareTo(person *array[7], int );
You need to add the definition in your program somewhere.
You define a function person::compareTo, which is different from the function compareTo declared in Test.cpp (because the former is a member function of person, while the latter is a global function).
Related
I have been looking in different threads with this error which is quite common but it feels like the IDE I am using messed with my workspace and I can't quite find the problem. I am setting up an extremely basic class called "Movie" that is specified below:
Movie.hpp :
#ifndef MOVIE_HPP
#define MOVIE_HPP
#include <iostream>
#include <string>
using std::string, std::cout,std::size_t;
class Movie
{
private:
std::string name;
std::string rating;
int watched_ctr;
public:
Movie(const string& name, const string& rating, int watched_ctr);
~Movie();
//getters
string get_name() const;
string get_rating() const;
int get_watched() const;
//setters
void set_name(string name);
void set_rating(string rating);
void set_watched(int watched_ctr);
};
#endif // MOVIE_HPP
Movie.cpp:
#include <iostream>
#include <string>
#include "Movie.hpp"
using std::string, std::cout,std::size_t,std::endl;
Movie::Movie(const string& name, const string& rating, int watched_ctr)
: name(name) , rating(rating) , watched_ctr(watched_ctr) {
}
Movie::~Movie()
{
cout << "Destructor for Movies class called /n";
}
//Getters
string Movie::get_name(){return name;}
string Movie::get_rating(){return rating;}
string Movie::get_watched(){return watched_ctr;}
//Setters
void Movie::set_name(std::string n){this -> name = n;}
void Movie::set_rating(std::string rating){this -> rating = rating;}
void Movie::set_watched(int ctr){this -> watched_ctr = ctr;}
The main.cpp I am trying only consists in creating one Movie object:
#include <iostream>
#include <string>
#include "Movie.hpp"
using std::string, std::cout,std::size_t,std::endl;
int main()
{
Movie StarTrek("Star Trek", "G", 20);
}
As you can see, I set all the attribute to private in order to exercise with the set/get methods but I keep stumbling upon the same error on each of them stating >"C:/Users/.../ProjectsAndTests/MoviesClass/Movie.cpp:18:8: error: no declaration matches 'std::__cxx11::string Movie::get_name()"
if you could give me a hint on what might cause this error I would greatly appreciate thank you!
I tried opening another workspace with classes implemented inside of them and the syntax I am using is very close from this test workspace I opened which compiled fine (no error regarding declaration match).
There are 2 problems with your code.
First while defining the member functions outside class you're not using the const. So to solve this problem we must use const when defining the member function outside the class.
Second, the member function Movie::get_watched() is declared with the return type of string but while defining that member function you're using the return type int. To solve this, change the return type while defining the member function to match the return type in the declaration.
//----------------------vvvvv--------->added const
string Movie::get_name()const
{
return name;
}
string Movie::get_rating()const
{
return rating;
}
vvv------------------------------>changed return type to int
int Movie::get_watched()const
{
return watched_ctr;
}
Working demo
I keep getting the Error C2228 left of '.topDisk' must have class/struct/union and I have no idea what it means, or even how to fix it, even after some research. Is it possible for someone to explain what the error is and how to fix it? I think I've provided you with all the code you need.
HanoiPegClass.cpp File
//A function moving one disk from one peg to another
void moveDisk(Peg& beginning, Peg& destination)
{
assert(beginning.getNumDisks() > 0);
if (destination.getNumDisks() > 0)
{
//Where the error is
assert(beginning.getNumDisks.topDisk() < destination.getNumDisks.topDisk());
}
destination.addDisk(beginning.topDisk());
beginning.removeDisk();
}
Peg.cpp File
//Function to return the disk count (amount of discs on each peg)
unsigned int Peg::getNumDisks()
{
return diskStack.size();
}
//Function to return the value of the top disk
int Peg::topDisk()
{
return diskStack.back();
}
Peg.h File
#pragma once
#include <vector>
#include <string>
using namespace std;
class Peg
{
private:
vector<int> diskStack;
string pegName;
void setName(string name);
public:
Peg(string name, int totalDisks);
unsigned int getNumDisks();
void printDisks();
string getName();
int topDisk();
void addDisk(int totalDisks);
int removeDisk();
~Peg();
};
getNumDisks is a function which returns an unsigned integer, so you cannot use a . on it. You can use the . operator on a structure or class or union only.
So change
assert(beginning.getNumDisks.topDisk() < destination.getNumDisks.topDisk());
to
assert(beginning.topDisk() < destination.topDisk());
Note in the following code the function called employee_dictionary() has not been created yet. If I highlight the error symbol next to the line number then the options I get from the Eclipse IDE are: function 'employee_dictionary' could not be resolved and 'employee_dictionary' was not declared in this scope.
I assumed I would be presented with the option of having Eclipse create this function automatically to resolve the error. Now I am left wondering if there is something fundamentally wrong with my code or if Eclipse does not have the functionality I am looking for.
I am new to c++ and Eclipse, and I am building this Employee class because it is usually where I start when I want to learn a new language; help resolving the issue would be appreciated. I want an IDE that has this capability so before I get to deep if I need to switch IDE's I will.
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
int id;
int salary;
public:
Employee(int new_id, int new_salary)
{
id = new_id;
salary = new_salary;
}
void setID(int newInt)
{
if (employee_dictonary(newInt) == 0)
{
id = newInt;
}
}
int getID()
{
return id;
}
void setSalary(int newInt)
{
salary = newInt;
}
int getSalary()
{
return salary;
}
};
int main()
{
std::cin.get();
}
You need to declare employee_dictionary as a function. You're trying to call a function that hasn't been defined yet. This is the same as trying to use a variable that you haven't defined
Example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::cout<<bob; // bob is not declared
}
In function 'int main()':
9:16: error: 'bob' was not declared in this scope
I am new to C++ and when I try to run this program it tells me: "error LNK2001: unresolved external symbol "private: static int Plate::plate_nID". I am right now just trying to create the plate class and print out the ID. Not sure where I went wrong.
#pragma once
#include <string>
using namespace std;
class Plate{
private:
int id;
string plateName;
static int plate_nID;
int generateID(){
plate_nID++;
return plate_nID;
}
public:
Plate(string name){
plateName = name;
id = generateID();
}
~Plate(){}
int getID(){
return id;
}
string getName(){
return plateName;
}
};
Here is my main:
#include "Plate.cpp"
#include "PlateNode.cpp"
using namespace std;
int main(){
Plate s=Plate::Plate("p1");
cout << s.getID();}
I have looked at this question:
Undefined reference to static class member
which similar questions to mine were marked as dulpicates of, but I when I try to do that it tells me: cannot instantiate non-static member outside of class. Please Help!
You need to define the static variable outside your class, only then will your code work
int Plate::plate_nID = 0;
You must define it outside the class (preferably outside the main() )
Probably better to define it right after your class.
You need to define the static member variable outside the class. Something along the lines of:
int Plate::plate_nID = 0;
I wanna make a pointer array that holds address of objects in that class so when i call scanner function it ll read pcode and search for objects has the same pcode. am i declaring array wrong? or did i misunderstand static concept? or something else ?
anyways i guess have to posting whole code
#include <string>
using namespace std;
class product{
public:
product();
product(long&,string&);
void setCode();
void getCode(long);
void static scanner();
void const printer();
static product *point[3];
static int a;
private:
string pname;
long pcode;
};/*
class PrepackedFood:public product{
public:
PrepackedFood(long&, string&,double);
private:
double uPrice;
};
class FreshFood:public product{
public:
FreshFood(long&,string&,double,double);
private:
double weight;
double pricepk;
};*/
#include "product.h"
#include <iostream>
product::product(){pcode=0;pname="unknown";
point[a]= this;
a++;}
product::product(long& c,string&n){pcode=c;pname=n;
}
//void const product::printer(){cout<<getCode()}
void product::setCode(){ cout<<"enter product name\n ";cin>>pname;
cout<<"enter product code _____\b\b\b\b\b";cout<<"\a";
cin>>pcode;cout<<endl;
cout<<pname<<endl;
cout<<pcode<<endl;
}
void product::getCode(long s){
if ((*this).pcode=s){
printer();
}
}
void product::scanner(){
long a;
cout<<"SCANNING!\a_____\b\b\b\b\b";cin>>a;
int i=0;
while(i<3){
if (point[i]->pcode==a){point[i]->printer();
break;
}
i++;
//(i==3)?cout<<"try again\n":"\a";
}
}
void const product::printer(){
cout<<pname<<endl;
cout<<pcode<<endl;
}
#include "product.h"
int main(){
product a[3];
int i=0;
while(i<3){
a[i].setCode();
i++;
}
product::scanner();
return 0;
}
i know it can be done a lot more easily i am just learning so just wanna fix scanner function. it doesn't compile
1>product.obj : error LNK2001: unresolved external symbol "public: static class product * * product::point" (?point#product##2PAPAV1#A)
1>product.obj : error LNK2001: unresolved external symbol "public: static int product::a" (?a#product##2HA)
The code looks like a mess.
The solution to your linker problem is in defining the already declared static point member:
product* product::point[3];
Is it not compiling, or is it compiling and crashing? Always say exactly what the problem is when posting. I can see some runtime problems in it easily though.
In your loop, you're always touching the pointers at point[0], point[1], and point[2]. However, you never initialize these to null or do null checks. So if you haven't called the constructor 3 times before calling scanner, you will segfault as one or more of these pointers will be invalid.
Also, your constructor never checks for overflow, so if you call the constructor more than 3 times it will segfault. And if you're ever passing objects back and forth directly from functions remember that the compiler may insert temporary object constructors.