0

I have a project where I need to expand some functionality without touching the project I'm using.

The core of the problem is this (I dumbed it down but the core should be clear).


//Layout.h
#ifndef LAYOUT_H
#define LAYOUT_H

#include "Speaker.h"

class Layout
{
    public:
        //unrelated stuff
    private:
        LSS::Speaker temp;
};
#endif

//Speaker.h"
#ifndef SPEAKER_H
#define SPEAKER_H
namespace LSS
{
    struct Speaker
    {
        Speaker(int val1, float val2)
        :some_int(val1)
        , some_float(val2)
        {
        }

        ~Speaker()    
        {
            //do other stuff
        }

         int some_int;
         float some_float;

    };//struct Speaker
}
#endif

I just want to be able to use the struct Speaker (as defined in "Speaker.h") in "Layout.h" without touching "Speaker.h"

If I compile I get following errors:

Error 22 error C2512: 'LSS::Speaker' : no appropriate default constructor > available Layout.cpp 7 1 MyProj

I think that wraps it up - any help would be appreciated, I'm pretty new to c++

Edit: in recreating the issue I made a TYPO (LSS: instead of LSS::). Updated the compiler error

8
  • 4
    LSS:Speaker temp; that should be LSS::Speaker temp; (note the :: instead of :) Commented Feb 5, 2015 at 11:27
  • This is so close to a testcase. Commented Feb 5, 2015 at 11:41
  • @SingerOfTheFall: y u answer in commentz??? Commented Feb 5, 2015 at 11:42
  • @Lightness, because I thought that might be a typo in the question itself. Commented Feb 5, 2015 at 11:44
  • it was indeed a typo in the question :) Commented Feb 5, 2015 at 11:49

3 Answers 3

1

Read carefully message

Error 3 error C2146: syntax error : missing ';' before identifier 'temp' > Layout.h 102 1

and then change LSS:Speaker temp; to LSS::Speaker temp;

EDIT:

For error

Error 22 error C2512: 'LSS::Speaker' : no appropriate default constructor > available Layout.cpp 7 1 MyProj

if you provide at least one constructor with parameters for Speaker, you also have to provide default constructor (constructor without parameters) to make LSS::Speaker temp; valid. It is a rule (for cases when no constructors are written by programmer compiler make default constructor, but if programmer start changing construction logic, compiler do nothing in this part of work).

Or you can provide default values in the existing constructor, like this:

  Speaker(int val1 = 0, float val2 = 0.0f)
        :some_int(val1)
        ,some_float(val2)
  {
  }
Sign up to request clarification or add additional context in comments.

8 Comments

And how would that error message be helpful? He is not missing a semicolon. Proper solution tho.
Single colon - is mistake. Double colon(::) have to be instead
I know... But the error message you stated tells that he's misssing a semicolon.
Most valuable in the error message is "syntax error"
Actually the answer is in comments to question
|
0

I figured out how to work around Layouts.h not having a default constructor (remember: in this case I wasn't allowed to edit the Speaker() struct)

In Layout.h I defined

class Speaker: public LSS::Speaker
{
    public:
        Speaker(): LSS::Speaker(-1, 0.0f){}
};

'Overloading' the missing constructor (that I couldn't fill in myself.

Problem solved :)

Comments

0

To answer the new question, now it's been changed to invalidate previous answers:

'LSS::Speaker' : no appropriate default constructor

As the error message says, the class has no default constructor, so it can only be instantiated by providing constructor arguments. You probably want to do this in the Layout constructor(s), for example:

Layout() : temp(some_value, some_other_value) {} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.