Small sample program gives me an error

Vaios Argiropoulos

My sample program is so small that i am going to give you all the source code below:

So here is ClassA header:

#ifndef CLASSA_H
#define CLASSA_H
#include <string>

class ClassA
{
public:
    ClassA(const std::string& fileName,  int TYPE = 0, float filter = 0.0f);
    ClassA(int width = 0, int height = 0, unsigned char* data = 0, int TYPE = 0, float   filter = 0.0f);
    ClassA(ClassA& classa);
    void operator=(ClassA& classa);
protected:

private:
    std::string m_lastBind;
    int m_textureTarget;
    bool m_freeTexture;
    int m_width;
    int m_height;
    float m_float;

};

#endif // CLASSA_H

Here is the source file for the ClassA:

#include "classa.h"

ClassA::ClassA(const std::string& fileName, int TYPE, float filter)
{
    m_lastBind = fileName;
    m_textureTarget = TYPE;
    m_float = filter;
}


static unsigned char whitePixel[] = {'A', 'B', 'B', 'A'};

ClassA::ClassA(int width, int height, unsigned char* data, int TYPE, float filter)
{
    m_width = width;
    m_height = height;
    data = whitePixel;
    m_textureTarget = TYPE;
    m_float = filter;
}


ClassA::ClassA(ClassA& classa)
{
    m_width = classa.m_width;
    m_height = classa.m_height;
    m_textureTarget = classa.m_textureTarget;
    m_float = classa.m_float;
}

Here is the header file for ClassB:

#ifndef CLASSB_H
#define CLASSB_H
#include <string>

class ClassB
{
    public:
        void someMethod(const int& num1, const int& num2, std::string& name);
    protected:

    private:
};

#endif // CLASSB_H

And finally the definition of the one and only method of ClassB is given below:

#include "classb.h"
#include "classa.h"

static unsigned char whitePixel[] = {0xFF, 0xFF, 0xFF, 0xFF};
void ClassB::someMethod(const int& num1, const int& num2, std::string& name)
{
    static ClassA WHITE = ClassA(1,1,whitePixel);
}

The main function is pretty much irrelevant because it is completely empty and has nothing to do with the errors i am getting from Code::Blocks. The errors are given below:

||=== Build: Debug in Testing2 (compiler: GNU GCC Compiler) ===|
classb.cpp||In member function ‘void ClassB::someMethod(const int&, const int&, std::string&)’:|
classb.cpp|8|error: no matching function for call to ‘ClassA::ClassA(ClassA)’|
classb.cpp|8|note: candidates are:|
classa.h|10|note: ClassA::ClassA(ClassA&)|
classa.h|10|note:   no known conversion for argument 1 from ‘ClassA’ to ‘ClassA&’|
classa.h|9|note: ClassA::ClassA(int, int, unsigned char*, int, float)|
classa.h|9|note:   no known conversion for argument 1 from ‘ClassA’ to ‘int’|
classa.h|8|note: ClassA::ClassA(const string&, int, float)|
classa.h|8|note:   no known conversion for argument 1 from ‘ClassA’ to ‘const string& {aka const std::basic_string<char>&}’|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
0x499602D2

This line:

static ClassA WHITE = ClassA(1,1,whitePixel);

needs a copy-constructor in order to work. The constructor ClassA(ClassA&) won't match because temporaries cannot bind to lvalue-references. You can make the constructor take a reference to const instead.

ClassA(ClassA const&);

This works because temporaries (rvalues) can bind to lvalue-references to const.


What were you were doing was creating a temporary ClassA value and copy-constructing WHITE from that. This is called copy-initialization. If you use direct-initialization instead:

static ClassA WHITE(1,1,whitePixel);

A copy-constructor simply copies all the members of the parameter to the object being created. Your code effectively does exactly the same thing. Fortunately the compiler will provide a copy-constructor for you by default, so you don't need to make one yourself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

This program gives me a circular import error but still works

From Dev

Sample SDL program gives an empty window

From Dev

MySQL 'AND' gives me error

From Dev

It gives me an error at GPS

From Dev

Error in OpenCV Edgeboxes sample program

From Dev

Dividing values gives me a small inaccuracy?

From Dev

Running Java Program in another Program gives error

From Dev

Running Java Program in another Program gives error

From Dev

my program runs but it gives me no output

From Dev

Animate UITableViewCell gives me error

From Dev

Sorting array gives me error

From Dev

php header gives me the error

From Dev

.getDownloadUrl() gives me error - Android

From Dev

using "this" in a fragment gives me an error

From Dev

Sorting array gives me error

From Dev

Animate UITableViewCell gives me error

From Dev

Firefox gives me dbus error

From Dev

Importing Android ContactsList sample project gives me somany errors

From Dev

Error in sample ruby program to swap element in an array

From Dev

Error in sample ruby program to swap element in an array

From Dev

Windows compilation error when running a small program

From Dev

Small error in my program C Language

From Dev

Windows compilation error when running a small program

From Dev

sklearn Ridge and sample_weight gives Memory Error

From Dev

Python program gives internal error: _select is undefined

From Dev

Program with embedded sql gives compilation error

From Dev

WordNet Export CSV Java program gives error

From Dev

Pip freeze gives me this error related with git

From Dev

Create empty array of struct gives me an error