views:

265

answers:

5

I have a bunch of warnings C4510 and C4610 when I use std::list with my class. It is a warning stating that default constructor is not available, and I want to disable them.

When I put:

#pragma warning(disable: 4510)

inside .cpp file that is instantiating this list nothing happens.

I tried placing this pragmas around function where I instantiate lists and even on top of the .cpp file but the results are the same - nothing happens. It only works if I disable warnings in properties dialog of .cpp file. I hate hiding stuff in properties like that because they get overlooked by developers. I would like to have them localized around the function. Is there something I could do about this?


EDIT:

Ok. This is how my code basically looks like. This code generates warnings 4510 and 4610 on warning level 4:

#include <list>

class foo {
public:
    foo(int) { }
};


class bar {};


class problem_class {
    foo m_foo;
    const bar *m_bar;
public:
    problem_class(const foo &_foo, const bar *_bar) : m_foo(_foo), m_bar(_bar) { }
};


void problem_fn(std::list<problem_class> &problem_collection) {
    foo _foo(3);
    problem_collection.clear();
    problem_collection.push_back(problem_class(_foo, new bar));
}


int main(int , char **)
{
    std::list<problem_class> collection;
    problem_fn(collection);

    return 0;
}
+1  A: 

Instead of hiding the problem by disabling warnings, how about wrapping your class w/o a default constructor in a proxy class that does have a default constructor*. The proxy's default constructor can then do the proper initialization of the wrapped class. Then store the proxy class in the std::list. This would make your intent clear and eliminate the warning.

*assuming you can't for whatever reason actually make the wrapped class have an appropriatte default constructor.

Doug T.
+1  A: 

You need to post some code that illustrates exactly what you are doing. Warning C4510 says:

The compiler cannot generate a default constructor for the specified class and no user-defined constructor was created. You will not be able to create objects of this type.

This doesn't seem to have anything to do with std::list, so it maybe that there is something wrong with your code.

I know this is not very helpful, but the code you posted looks fine to me and compiled with no warnings with g++ and comeau. I don't use VC++ anymore, so can't reall help further, I'm afraid.

Further Edit: Purely in the spirit of experimentation, what happens if you change:

const bar *m_bar;

to

bar *m_bar;

The MSDN docs for this warning say that:

There are several situations that prevent the compiler from generating a default constructor, including:

* A const data member.

Now the m_bar member isn't const (the thing it points to is) but I wonder if the compiler is a little confused about this.

anon
Container classes require that there be copy constructors and assignment operators. They do not appear to require default constructors. It is of course possible that VC++ wants to see them in std::list, since VC++ does have its faults.
David Thornley
I know it looks ok. :) I also found this: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=180610 . That is why I am trying to disable the warnings, but the problem is that #pragma is not working. In this small example it seems that warnings can be disabled by placing #pragma in front of #include<list>, but in my code it doesn't work even if I place it on top of the .cpp file. I hoped that someone encountered the same issue before.
bombardier
Sucha requirement would make std::list (or any other std container) pretty useless.
anon
No, the const is not an issue.
bombardier
+2  A: 

Include #pragma before including <list>

#pragma warning (disable:4510)
#pragma warning (disable:4610)
#include <list>
aJ
+1  A: 
#pragma warning (disable : 4510 4610)
#pragma warning (push, 3)
#include <list>
#pragma warning (pop)
#pragma warning (default : 4510 4610)
EvilTeach
+1  A: 

Ok, found it.

My project uses precompiled headers so in some header file that is included from StdAfx.h someone included list. When I added #pragma directives on top of the StdAfx.h everything worked. The thing that confused me is that when I added #pragma in .cpp file in front of

#include "StdAfx.h"

nothing worked (warnings were still displayed). Since list was included in precompiled headers, it had the same warning settings no matter what the .cpp file specified later on.

But, the strange thing is that even if I could not override settings in .cpp file, I could override them by specifying compile properties for that same file. How is that any different?

bombardier
Ya, the compiler is anal about that include file.It must be physically the first item in each of the files.
EvilTeach

related questions