简体   繁体   中英

C++ Undefined reference to functions

I hope you will help me. So,I have this class and whenever I try to compile the main.cpp I get the errors: Undefined reference to 'Sally::Sally()', to 'void Sally::print()' and twice for 'Sally::~Sally()'

here is my header file:

#ifndef SALLY_H
#define SALLY_H


class Sally
{
    public:
        Sally();
        void print();
        virtual ~Sally();
    protected:
    private:
};

#endif // SALLY_H

here is my Sally.cpp file

#include<iostream>
#include "Sally.h"
using namespace std;

Sally::Sally()
{

}
void Sally::print()
{
  cout<<"print something"<<endl;
}

Sally::~Sally()
{

}

here is my main.cpp file

#include <iostream>
#include"Sally.h"
using namespace std;

int main()
{
    Sally salObj;
    salObj.print();

}

I saw a comment here that I should include the 3 files in a project but whenever I do I make a Console Application and it prints out "Hello world" even though I don't even have that anywhere. I'd love if someone can help me,I've been bugged with this for a couple of days and nothing seems to make it better.

Code::Blocks adds a default main.cpp file to a Console Application project. You need to remove that file, and add your own files by right clicking on the project name -> "Add files...". Make sure that you add all three files.

删除main.cpp添加文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM