944,144 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 748
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 5th, 2009
0

Need help with an error that I don't know how to fix, and have never seen it before.

Expand Post »
When I try to compile my code, I get this error:
C++ Syntax (Toggle Plain Text)
  1. ------ Build started: Project: SUD, Configuration: Debug Win32 ------
  2. Compiling...
  3. baseent.cpp
  4. c:\program files\microsoft visual studio 9.0\vc\include\xmemory(52) : error C2558: class 'BaseEnt' : no copy constructor available or copy constructor is declared 'explicit'
  5. c:\program files\microsoft visual studio 9.0\vc\include\xmemory(155) : see reference to function template instantiation 'void std::_Construct<BaseEnt,_Ty>(_T1 *,const _T2 &)' being compiled
  6. with
  7. [
  8. _Ty=BaseEnt,
  9. _T1=BaseEnt,
  10. _T2=BaseEnt
  11. ]
  12. c:\program files\microsoft visual studio 9.0\vc\include\xmemory(154) : while compiling class template member function 'void std::allocator<_Ty>::construct(BaseEnt *,const _Ty &)'
  13. with
  14. [
  15. _Ty=BaseEnt
  16. ]
  17. c:\program files\microsoft visual studio 9.0\vc\include\vector(429) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
  18. with
  19. [
  20. _Ty=BaseEnt
  21. ]
  22. c:\program files\microsoft visual studio 9.0\vc\include\vector(439) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled
  23. with
  24. [
  25. _Ty=BaseEnt,
  26. _Alloc=std::allocator<BaseEnt>
  27. ]
  28. c:\documents and settings\tom\my documents\visual studio 2008\projects\sud\sud\baseent.h(25) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
  29. with
  30. [
  31. _Ty=BaseEnt
  32. ]
  33. Generating Code...
  34. Compiling...
  35. main.cpp
  36. Generating Code...
  37. Build log was saved at "file://c:\Documents and Settings\tom\My Documents\Visual Studio 2008\Projects\SUD\SUD\Debug\BuildLog.htm"
  38. SUD - 1 error(s), 0 warning(s)
  39. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I clicked on it, and it pointed me to this line of code:
extern std::vector<BaseEnt> entities;
This is where BaseEnt is defined:

C++ Syntax (Toggle Plain Text)
  1. class BaseEnt{
  2. int id;
  3. static int count;
  4. std::string hashcode;
  5. std::string name;
  6. public:
  7. BaseEnt();
  8. BaseEnt(std::string name);
  9. BaseEnt(BaseEnt &other);
  10. ~BaseEnt();
  11.  
  12. int GetID();
  13. std::string GetHashCode();
  14. std::string GetClassName();
  15. int GetCount();
  16. };

This is where it's member definitions are:
C++ Syntax (Toggle Plain Text)
  1. int BaseEnt::count = 1;
  2.  
  3. std::vector<BaseEnt> entities(;
  4.  
  5. BaseEnt::BaseEnt()
  6. :id(count), name("unassigned"), hashcode(HashCode())
  7. {
  8. name += HashCode();
  9. entities[id] = *this;
  10. count++;
  11. }
  12.  
  13. BaseEnt::BaseEnt(std::string classname)
  14. :id(count), name(classname), hashcode(HashCode())
  15. {
  16. entities[id] = *this;
  17. count++;
  18. }
  19.  
  20. BaseEnt::BaseEnt(BaseEnt &other)
  21. :id(other.GetID()), name(other.GetClassName()), hashcode(other.GetHashCode())
  22. {
  23. entities[id] = *this;
  24. count++;
  25. }
  26.  
  27. BaseEnt::~BaseEnt()
  28. {
  29. count--;
  30. }
  31.  
  32. std::string BaseEnt::GetClassName()
  33. { return name; }
  34. int BaseEnt::GetCount()
  35. { return BaseEnt::count; }
  36. std::string BaseEnt::GetHashCode()
  37. { return hashcode; }
  38. int BaseEnt::GetID()
  39. { return id; }

I have never seen this error, and I can't figure out what it means, because I've got a copy constructor in my class(line 9).

And help will be appreciated.
Last edited by tomtetlaw; Nov 5th, 2009 at 2:41 am.
Similar Threads
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Nov 5th, 2009
0
Re: Need help with an error that I don't know how to fix, and have never seen it before.
Bump.
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Nov 5th, 2009
1
Re: Need help with an error that I don't know how to fix, and have never seen it before.
Hey Tom.
Looking at the error messages and the fact that some of them are referring to a vector and then looking at line 3 of your final block of code:
C++ Syntax (Toggle Plain Text)
  1. std::vector<BaseEnt> entities(;
Note: You haven't closed the brackets ')' !!

I'm beginning to wonder if the compiler is getting a bit confused and is thinking that the code after the opening bracket are arguments to the constructor for the vector of BaseEnt's...Perhaps throwing the errors you're seeing....

What happens after you close the brackets and then try to recompile??
C++ Syntax (Toggle Plain Text)
  1. std::vector<BaseEnt> entities();

Give that a shot and see what happens!

Cheers for now,
Jas.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Nov 5th, 2009
0
Re: Need help with an error that I don't know how to fix, and have never seen it before.
That was just an error by me when copying and pasting it.
This is what it looks now:

std::vector<BaseEnt> entities(MAX_ENTITIES);
MAX_ENTITIES is a macro with the value of 100000.

Any help will and has been appreciated : )
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Nov 6th, 2009
0
Re: Need help with an error that I don't know how to fix, and have never seen it before.
Bump, This is kinda urgent by the way.
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Nov 6th, 2009
0
Re: Need help with an error that I don't know how to fix, and have never seen it before.
OK, so looking at your original post again, you said that clicking on the error brought you to this line of code:
extern std::vector<BaseEnt> entities; This code looks like it's from another file which you haven't posted any other code for (apart from the above line).

Looking at the errors I'm thinking that where your extern is declared, perhaps the compiler is trying to copy the original 'entities' variable and the STL code is for some reason having difficulty finding an appropriate copy constructor for the vector of BaseEnts...

So looking at the line of code that produces the error, you've got 'entities' declared as an extern, meaning that the variable is declared externally (obvioulsy in BaseEnt.cpp).

Now looking at the declaration of 'entities' in BaseEnt.cpp, one thing immediately strikes me...If you are using this variable externally in other .cpp files/classes, shouldn't it be static? I wonder if that might be causing the errors you're seeing. As 'entities' is not static in BaseEnt.cpp, who's to say that its even in scope when the extern is declared in your other .cpp file? Perhaps this is causing the STL code to trip up!

So making 'entities' static in BaseEnt.cpp might be worth a trying for starters.

I'd also consider perhaps making it static and putting it into an unnamed namespace in BaseEnt.cpp (making it private to the BaseEnt module) and then creating a static accessor function in BaseEnt which can return a pointer or reference to 'entities' (BaseEnt::GetEntities()?).

That way you won't have to declare 'entities' as an external variable in your other classes/files which need to use it, you'd just use the BaseEnt::GetEntities() function to get a pointer or reference to the entities variable instead. It would make your code more readable, and when debugging you'd also easily be able to track which parts of your code were using/modifying 'entities' by doing a search for instances of BaseEnt::GetEntities().

Depending on how you're going to be using entities in your other classes, you might also want to consider creating a version that will return a const pointer or const reference too!

Other than that, I'm not sure what to suggest. It definitely appears to be some kind of STL related error. I've not seen these errors before either, so I'm not entirely sure what is causing the problem.

Anyway, that's me pretty much out of ideas!
Cheers for now,
Jas.
[EDIT]
p.s. If you have any further problems, perhaps .zip up and post your entire project and I'm sure someone can do some digging!
Last edited by JasonHippy; Nov 6th, 2009 at 6:10 am.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Nov 6th, 2009
0
Re: Need help with an error that I don't know how to fix, and have never seen it before.
Making it static didn't work, I'll attach the project in a .zip.
Attached Files
File Type: zip project.zip (457.0 KB, 17 views)
Reputation Points: 9
Solved Threads: 5
Posting Pro
tomtetlaw is offline Offline
591 posts
since Sep 2008
Nov 6th, 2009
0
Re: Need help with an error that I don't know how to fix, and have never seen it before.
I haven't got VS2008, so I can't load the original project. However, I've bunged the files into a VS2003 project and attempted to compile.

I can see the errors you're getting and I've noticed that if I comment out the copy constructor, those wierd errors are not thrown. So there is something about your copy constructor that the compiler doesn't like.

Offhand I'm still not sure exactly what the problem is though!

Obviously, because you've got that static counter that gets altered every time a new BaseEnt is created or destroyed, you need a custom copy constructor...So sticking with the default copy constructor is out of the question.

hmmm....More digging is in order...

BTW: Alongside commenting out the copy constructor, I've made the following edits to baseent.h:
C++ Syntax (Toggle Plain Text)
  1. #pragma once
  2.  
  3. #include <vector>
  4. #include <string>
  5.  
  6. #define MAX_ENTITIES 500
  7.  
  8. class BaseEnt{
  9. int id;
  10. std::string hashcode;
  11. std::string name;
  12. public:
  13. BaseEnt();
  14. BaseEnt(std::string name);
  15. //BaseEnt(BaseEnt &other);
  16. ~BaseEnt();
  17.  
  18. int GetID();
  19. int GetCount();
  20. std::string GetHashCode();
  21. std::string GetName();
  22. };
  23.  
  24. namespace // contents are local/private to this module
  25. {
  26. // The statics only need to be declared here in the header.
  27. static std::vector<BaseEnt> entities(MAX_ENTITIES);
  28. static int count=0;
  29. }
  30. // allow modules outside of baseent.cpp to be able to access the list of entities...
  31. static std::vector<BaseEnt>& GetEntities(){return entities;}
  32.  
  33. BaseEnt FindClass_FromName (std::string classname);
  34. BaseEnt FindClass_FromHash (std::string classhash);
  35. BaseEnt FindClass_FromID (int classid);
  36. BaseEnt FindClass_FromCount (int classcount);

and baseent.cpp:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include "baseent.h"
  5. #include "common_func.h"
  6.  
  7.  
  8. BaseEnt::BaseEnt()
  9. :id(count), name("unassigned"), hashcode(HashCode())
  10. {
  11. name += hashcode;
  12. entities[id] = *this;
  13. count++;
  14. }
  15.  
  16. BaseEnt::BaseEnt(std::string classname)
  17. :id(count), name(classname), hashcode(HashCode())
  18. {
  19. entities[id] = *this;
  20. count++;
  21. }
  22.  
  23. /*BaseEnt::BaseEnt(BaseEnt &other)
  24. :id(other.GetID()), name(other.GetName()), hashcode(other.GetHashCode())
  25. {
  26. entities[id] = *this;
  27. count++;
  28. }*/
  29.  
  30. BaseEnt::~BaseEnt()
  31. {
  32. count--;
  33. }
  34.  
  35. std::string BaseEnt::GetName()
  36. { return name; }
  37. int BaseEnt::GetCount()
  38. { return count; }
  39. std::string BaseEnt::GetHashCode()
  40. { return hashcode; }
  41. int BaseEnt::GetID()
  42. { return id; }
  43.  
  44.  
  45.  
  46. BaseEnt FindClass_FromCount(int classcount)
  47. {
  48. for(int i = 0; i < MAX_ENTITIES; ++i)
  49. if(entities[i].GetCount() == classcount)
  50. return entities[i];
  51. return 0;
  52. }
  53.  
  54. BaseEnt FindClass_FromHash(std::string classhash)
  55. {
  56. for(int i = 0; i < MAX_ENTITIES; ++i)
  57. if(entities[i].GetHashCode() == classhash)
  58. return entities[i];
  59.  
  60. return 0;
  61. }
  62.  
  63. BaseEnt FindClass_FromID(int classid)
  64. {
  65. for(int i = 0; i < MAX_ENTITIES; ++i)
  66. if(entities[i].GetID() == classid)
  67. return entities[i];
  68.  
  69. return 0;
  70. }
  71.  
  72. BaseEnt FindClass_FromName(std::string classname)
  73. {
  74. for(int i = 0; i < MAX_ENTITIES; ++i)
  75. if(entities[i].GetName() == classname)
  76. return entities[i];
  77.  
  78. return 0;
  79. }
Cheers for now,
Jas.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Nov 6th, 2009
0
Re: Need help with an error that I don't know how to fix, and have never seen it before.
Ah...Of course!

The parameter to your copy constructor should be const..
e.g.
C++ Syntax (Toggle Plain Text)
  1. BaseEnt(const BaseEnt &other);

Also you need to alter the signatures of GetName, GetHashCode and GetID so they are const.

So your baseent.h needs to look like this:
C++ Syntax (Toggle Plain Text)
  1. #pragma once
  2.  
  3. #include <vector>
  4. #include <string>
  5.  
  6. #define MAX_ENTITIES 500
  7.  
  8. class BaseEnt{
  9. int id;
  10. std::string hashcode;
  11. std::string name;
  12. public:
  13. BaseEnt();
  14. BaseEnt(std::string name);
  15. BaseEnt(const BaseEnt& other);
  16. ~BaseEnt();
  17.  
  18. int GetID() const;
  19. static int GetCount();
  20. std::string GetHashCode() const;
  21. std::string GetName() const;
  22. };
  23.  
  24. namespace
  25. {
  26. // The statics only need to be declared here in the header.
  27. static std::vector<BaseEnt> entities(MAX_ENTITIES);
  28. static int count=0;
  29. }
  30. // allow modules outside of baseent.cpp to be able to access the list of entities...
  31. static std::vector<BaseEnt>& GetEntities(){return entities;}
  32.  
  33. BaseEnt FindClass_FromName (std::string classname);
  34. BaseEnt FindClass_FromHash (std::string classhash);
  35. BaseEnt FindClass_FromID (int classid);
  36. BaseEnt FindClass_FromCount (int classcount);


And baseent.cpp like so:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include "baseent.h"
  5. #include "common_func.h"
  6.  
  7.  
  8. BaseEnt::BaseEnt()
  9. :id(count), name("unassigned"), hashcode(HashCode())
  10. {
  11. name += hashcode;
  12. entities[id] = *this;
  13. count++;
  14. }
  15.  
  16. BaseEnt::BaseEnt(std::string classname)
  17. :id(count), name(classname), hashcode(HashCode())
  18. {
  19. entities[id] = *this;
  20. count++;
  21. }
  22.  
  23. BaseEnt::BaseEnt(const BaseEnt& other)
  24. :id(other.GetID()), name(other.GetName()), hashcode(other.GetHashCode())
  25. {
  26. entities[id] = *this;
  27. count++;
  28. }
  29.  
  30. BaseEnt::~BaseEnt()
  31. {
  32. count--;
  33. }
  34.  
  35. std::string BaseEnt::GetName() const
  36. { return name; }
  37. int BaseEnt::GetCount()
  38. { return count; }
  39. std::string BaseEnt::GetHashCode() const
  40. { return hashcode; }
  41. int BaseEnt::GetID() const
  42. { return id; }
  43.  
  44.  
  45.  
  46. BaseEnt FindClass_FromCount(int classcount)
  47. {
  48. for(int i = 0; i < MAX_ENTITIES; ++i)
  49. if(entities[i].GetCount() == classcount)
  50. return entities[i];
  51. return 0;
  52. }
  53.  
  54. BaseEnt FindClass_FromHash(std::string classhash)
  55. {
  56. for(int i = 0; i < MAX_ENTITIES; ++i)
  57. if(entities[i].GetHashCode() == classhash)
  58. return entities[i];
  59.  
  60. return 0;
  61. }
  62.  
  63. BaseEnt FindClass_FromID(int classid)
  64. {
  65. for(int i = 0; i < MAX_ENTITIES; ++i)
  66. if(entities[i].GetID() == classid)
  67. return entities[i];
  68.  
  69. return 0;
  70. }
  71.  
  72. BaseEnt FindClass_FromName(std::string classname)
  73. {
  74. for(int i = 0; i < MAX_ENTITIES; ++i)
  75. if(entities[i].GetName() == classname)
  76. return entities[i];
  77.  
  78. return 0;
  79. }

That should get rid of all of those rather nasty errors!

Cheers for now,
Jas.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Nov 6th, 2009
0
Re: Need help with an error that I don't know how to fix, and have never seen it before.
Here's your original project back, I've included my edits in there.

See attached .zip.

Cheers for now,
Jas.
Attached Files
File Type: zip JasEdits.zip (453.7 KB, 15 views)
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dr. MyName, or how I learned to stop worrying and love using strings in functions
Next Thread in C++ Forum Timeline: Multi-Threading





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC