0

i am trying to compile multiple .cpp and .h file. But it show error.

My files are:

ApplicationWindow.cpp

#include "ApplicationWindow.h"

void ApplicationWindow::DrawContents ()
{
  GetView()->DrawOn(this);
}

ApplicationWindow.h

#ifndef APPLICATION_WINDOW_H
#define APPLICATION_WINDOW_H

#include "Window.h"

class ApplicationWindow : public Window
{
public:
    // ...
    virtual void DrawContents();
};

#endif /* APPLICATION_WINDOW_H */

Window.h

#ifndef WINDOW_H
#define WINDOW_H

#include "View.h"

class Point;
class WindowImp;

class Window {
public:
    Window(View* contents);

    // requests handled by window
    virtual void DrawContents();

    virtual void Open();
    virtual void Close();
    virtual void Iconify();
    virtual void Deiconify();

    // requests forwarded to implementation
    virtual void SetOrigin(const Point& at);
    virtual void SetExtent(const Point& extent);
    virtual void Raise();
    virtual void Lower();

    virtual void DrawLine(const Point&, const Point&);
    virtual void DrawRect(const Point&, const Point&);
    virtual void DrawPolygon(const Point[], int n);
    virtual void DrawText(const char*, const Point&);

protected:
    WindowImp* GetWindowImp();
    View* GetView();

private:
    WindowImp* _imp;
    View* _contents; // the window's contents
};

#endif /* WINDOW_H */

Window.cpp

#include "Window.h"
#include "WindowImp.h"
#include "WindowSystemFactory.h"

void Window::DrawRect (const Point& p1, const Point& p2) {
    WindowImp* imp = GetWindowImp();
    imp->DeviceRect(p1.X(), p1.Y(), p2.X(), p2.Y());
}

WindowImp* Window::GetWindowImp () {
    if (_imp == 0) {
        _imp = WindowSystemFactory::Instance()->MakeWindowImp();
    }
    return _imp;
}

View.h

#ifndef VIEW_H
#define VIEW_H

class ApplicationWindow;

class View {
public:
    void DrawOn(const ApplicationWindow* w) const;
};

#endif /* VIEW */

View.cpp

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

void View::DrawOn(const ApplicationWindow* w) const
{
    cout << "DrawOn(" << w << ")" << endl;
}

both WindowImp.h and WindowSystemFactory.h file also i have.

I tried in all the way which is i found from other stackoverflow answers (Using G++ to compile multiple .cpp and .h files.

But it show the following error:

"Window::DrawPolygon(Point const*, int)", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::Open()", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::Close()", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::Lower()", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::Raise()", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::GetView()", referenced from:

  ApplicationWindow::DrawContents() in ApplicationWindow.o

"Window::Iconify()", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::DrawLine(Point const&, Point const&)", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::DrawRect(Point const&, Point const&)", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::DrawText(char const*, Point const&)", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::Deiconify()", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::SetExtent(Point const&)", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"Window::SetOrigin(Point const&)", referenced from:

  vtable for ApplicationWindow in ApplicationWindow.o

"View::DrawOn(ApplicationWindow const*) const", referenced from:

  ApplicationWindow::DrawContents() in ApplicationWindow.o

"typeinfo for Window", referenced from:

  typeinfo for ApplicationWindow in ApplicationWindow.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

And my g++ --version

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix

I am using Mac OSX 10.9.4. I know this is duplicate of above mentioned answer, but still i not able to solve. If any one help me it will great to me.

I have to just run g++ ApplicationWindow.cpp file without above error. but i tried to link all other .cpp and .h file the way mentioned in answer of above the link which i pasted over here. Thank in advance....

Community
  • 1
  • 1
nagarajan
  • 474
  • 3
  • 21

1 Answers1

1

You declare a lot of virtual functions in your Window. header but you are not defining any implementations.

You can compile each .cpp file since there are no syntax errors in there. However the linker can't find the functions you are declaring and can't produce an executable as a result.

http://www.parashift.com/c++-faq/link-errs-missing-vtable.html

Simply start by hiding all the virtual functions you are not using and not defining then try again. (Or add empty implementations to them)

odedsh
  • 2,594
  • 17
  • 17
  • but in Window.cpp file i implemented `Window::DrawRect` but for that also i ma getting same error above right. – nagarajan Feb 13 '15 at 12:53
  • Window vtable should contain functions such as "Window::Deiconify()" , "Window::Close()"... these functions are not found anywhere.. – odedsh Feb 13 '15 at 12:54
  • Simply start by hiding all the virtual functions you are not using and not defining then try again. (Or add empty implementations to them) – odedsh Feb 13 '15 at 13:05
  • i implemented all virtual function definition, can u tell me the order of g++ command, becz still showing the same error – nagarajan Feb 13 '15 at 13:54
  • 1
    g++ -Wall .. -o test.exe Although if you are working from a makefile you should probably compile each file seperately into an .o file (using -c flag) Then link all the .o files together in a separate step – odedsh Feb 15 '15 at 10:30