Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Amazing solutions for the: ''c++ undefined reference to constructor'' error.
Next
Answered
Quinn Lee
  • 13
Quinn Lee
Asked: May 18, 20222022-05-18T18:37:21+00:00 2022-05-18T18:37:21+00:00In: cpp

Amazing solutions for the: ”c++ undefined reference to constructor” error.

  • 13

. Advertisement .

..3..

. Advertisement .

..4..

As advised, I used some code samples in another forum, but it could not improve the problem. My question is the “c++ undefined reference to constructor” in cpp-how to solve it? The command line is:

...
 #include "GameObjects/StaticObject.cpp"
 ...
...
 
 #include <SFML/Graphics.hpp>
 #include "Graphics/Graphics.hpp"
 #include "GameObjects/StaticObject.hpp"
 
 int main(int argc, char** argv) {
 
  //SETUP
  Graphics graphics;
 
  background = new StaticObject(&graphics, "Data/Images/BackgroundPlaceholder.png", sf::Vector2f(0,0));
 
 ...
...
 
 #include <SFML/Graphics.hpp>
 #include "GameObjects/StaticObject.hpp"
 
 ...
 
 // Objects
 StaticObject *background;
 ...
#include <SFML/Graphics.hpp>
 #include "../Graphics/Graphics.hpp"
 
 class StaticObject{
 public:
  StaticObject();
  StaticObject(Graphics *_graphics, sf::String texture_filename, sf::Vector2f _position);
  StaticObject(const StaticObject& orig);
  virtual ~StaticObject();
 private:
  // The sprite stores the position
  sf::Sprite *sprite;
  sf::Texture *texture;
 };
#include "StaticObject.hpp"
 #include <SFML/Graphics.hpp>
 #include "../Graphics/Graphics.hpp"
 
 StaticObject::StaticObject(){ 
 }
 
 StaticObject::StaticObject(Graphics *_graphics, sf::String texture_filename, sf::Vector2f _position) {
  sprite = _graphics->AddSprite(texture_filename);
  sprite->setPosition(_position);
 }
 
 StaticObject::StaticObject(const StaticObject& orig) {
 }
 
 StaticObject::~StaticObject() {
 }
#include "GameObject/StaticObject.cpp"

and the result:

main.cpp:20: undefined reference to `StaticObject::StaticObject(Graphics*, sf::String,
sf::Vector2)'

What does the message mean? Can you advise me to fix it? If you have other better answers, leave them in the answer box below.

debugging
  • 2 2 Answers
  • 82 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-16T01:45:44+00:00Added an answer on June 16, 2022 at 1:45 am

    The cause:

    The error occured because the linker was unable to find the definition of a function/method (in this case, constructor).

    StaticObject::StaticObject(Graphics*, sf::String, sf::Vector2<float>)

    When you added the cpp file, the compiler caused that code as part of the file (you should not do this).

    These are the reasons of this error.

    Solution:

    You need to link the two objects together after compiling the GameObject/StaticObject.cpp code as its own object.

    Or you can add all of the.cpp files to a single project so that Netbeans can link all of the.o files together into a single executable. If you make StaticObject.cpp into its own library, you need to specify the path to that library so that the linker can locate the implementation.

    When you make your own program, it’s really great if these lines appear:

    Compile: StaticObject.cpp -> StaticObject.o
    Compile: main.cpp -> main.o
    Link: StaticObject.o, main.o -> main_program

    Although, if you specify all the source files (and any libraries) in the same command line, gcc/g++ can bypass all the intermediate.o file generations by many ways and build the main_program immediately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Ambre Serre
    2022-05-25T20:47:26+00:00Added an answer on May 25, 2022 at 8:47 pm

    The linker could not find StaticObject‘s definition, so you have not compiled or linked StaticObject.cpp. Each.cpp file must be separately compiled. The object files that the compiler creates for each one should be provided to the linker.

    StaticObject.cpp is included in Main.cpp because you tell the preprocessor that it will insert the contents of StaticObject.cpp inside Main.cpp. This allows you to compile the definitions into Main.cpp’s output.

    • 8
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.