Strange compile errors...

DSP, Plugin and Host development discussion.
RELATED
PRODUCTS

Post

Hi,
I'm using VC++ 6.0,
I have a struct and I want to add to a std::list struct pointers.

#include <list>
using std::list;

typedef struct {
int time, note, velocity;
} Note_Event;

...

typedef std::list <Note_Event *> m_events;

....

Note_Event *event;
event = new Note_Event;

m_events.push_back(event);

I get this error :

c:\vstsdk\vstsdk2\source\vstxsynth2\event.cpp(51) : error C2065: 'm_events' : undeclared identifier
c:\vstsdk\vstsdk2\source\vstxsynth2\event.cpp(51) : error C2228: left of '.push_back' must have class/struct/union type

I tryed a lot of tips I read on the net, but it didn't fix it.

Post

I think the line:

Code: Select all

typedef std::list <Note_Event *> m_events;
should lose the "typedef" - as written, you're creating a new type m_events rather than a list-of-events object.
Image
Don't do it my way.

Post

Borogove wrote:I think the line:

Code: Select all

typedef std::list <Note_Event *> m_events;
should lose the "typedef" - as written, you're creating a new type m_events rather than a list-of-events object.
No it's not that. I introduced the "typedef" trying to fix the bug. It looks like the micro$oft compiler is not aware that in the line

Code: Select all

std::list <Note_Event *> m_events;
a new list is created under the name m_events, and type struct pointer. Yesterday I was google 3 hours reading forums and tips but i didn't find a solution that actually works.
Kind regards,
A.F.

Post

With typedef you define a new type, not a new instance of list. You have to instanciate this new type afterwards to create a list object. So it should be :

Code: Select all

typedef std::list <Note_Event *> EventList;
EventList m_events;

...

Note_Event* event = new Note_Event;
m_events.push_back(event);
-- Laurent

Post

using std::list; ???

list is not a name-space...

in the h file

Code: Select all

include <list>
..

class Arne
{
  void Method();
  std::list<Note_Event *> m_events;
}
in cpp file

Code: Select all

void
Arne::Method()
{
  Note_Event *event; 
  event = new Note_Event; 

  m_events.push_back(event);
}
Stefan H Singer
Musician, coder and co-founder of We made you look Web agency

Post

quote : using std::list; ???

yes.

in the h file

Code: Select all

include <list>
..

class Arne
{
  void Method();
  std::list<Note_Event *> m_events;
}
in cpp file

Code: Select all

void
Arne::Method()
{
  Note_Event *event; 
  event = new Note_Event; 

  m_events.push_back(event);
}
tryed that way too. The only difference in your code, is that I #include <list> in the cpp file.
Thanks, I will try this one too.

Post

edit : This is my class...

Code: Select all

#include <math.h>
#include <stdio.h>
//#include <dir.h>
#include <string.h>
#include <fstream.h>
#include <windows.h>
#include "Event.h"

#include "vstcontrols.h"

#include <list>


using std::list;

		typedef struct {
		int time, note, velocity;
		} Note_Event;	

EventQueue::EventQueue()
{

	typedef std::list <Note_Event *> m_events;
};

EventQueue::~EventQueue()
{

};

void EventQueue::addNote(int time, int note, int velocity)
{
Note_Event *event;
event = new Note_Event;

event->time		= time;
event->note		= note;
event->velocity = velocity;

m_events.push_back(event);

};

int EventQueue::getEventCount()
{

return 0;
};

int EventQueue::getNextEventTime()
{

return 0;
};

int EventQueue::getNextEvent()
{

return 0;
};


bool EventQueue::isEmpty()
{


return false;
}

Code: Select all

#ifndef __events__
#define __events__

class EventQueue 
{

public:

	EventQueue();
	~EventQueue();

protected :
	
		


	virtual void addNote(int time, int note, int velocity);
	virtual int getEventCount();
	virtual int getNextEventTime();// return time of next event in queue without removing the event
	virtual int getNextEvent();// return next event and remove from queue

	virtual bool isEmpty(void);
	

private:

};

#endif 




[/size][/code]

Post

You are declaring m_events as a local _TYPE_ (due to typedef, without typedef it would be a local variable which still is wrong :)) in a method instead of declaring it as a member of the class. When declaring it in a method, no other methods could possibly know that this variable exists (and it does not exist outside of the method where you declared it).

declare it as a private member instead, like this
(and get rid of using std::list, list is not a namespace)

Code: Select all

#ifndef __events__ 
#define __events__ 

class EventQueue 
{ 

public: 

   EventQueue(); 
   ~EventQueue(); 

protected : 
    
       


   virtual void addNote(int time, int note, int velocity); 
   virtual int getEventCount(); 
   virtual int getNextEventTime();// return time of next event in queue without removing the event 
   virtual int getNextEvent();// return next event and remove from queue 

   virtual bool isEmpty(void); 
    

private: 
   std::list<Note_Event*> m_events;

}; 

#endif 
Stefan H Singer
Musician, coder and co-founder of We made you look Web agency

Post

That's exactly where I started from. I declared it like that :

Code: Select all

private: 
   std::list<Note_Event*> m_events; 
but I had those 2 errors anyway.
Damn, I don't have a decent compiler at work :x
Btw. Does it matter if I dont instantiate the EventQueue class at all ?
Thanks,
A.F.

Post

You got to have the class declaration before the class definition (implementation).

What do you mean about the instantiation thingy? How could a method use members that does not exist?
Stefan H Singer
Musician, coder and co-founder of We made you look Web agency

Post

stefancrs wrote:You got to have the class declaration before the class definition (implementation).

What do you mean about the instantiation thingy? How could a method use members that does not exist?
What I mean is, that the EventQueue class is not used in code at all, like :

my_queue = new EventQueue();

Normally I would call that from initProcess ();
And I was asking if that matters in this case.

Post

No, the class should compile without being used anywhere.

If you send me the whole class at hallen_stefan at yahoo.se I could take a look at it.
Stefan H Singer
Musician, coder and co-founder of We made you look Web agency

Post

Fixed!

Only things I needed to change was to move the struct, the m_events declaration to the header file.
Stefan H Singer
Musician, coder and co-founder of We made you look Web agency

Post

stefancrs wrote:Fixed!

Only things I needed to change was to move the struct, the m_events declaration to the header file.
It was the #include <list>
I had to include it in the header file too.
Anyway, I have more beginner problems.
One of my methods must return a pointer to a "clone" of the Note_Event struct. (clone, i mean clone = new Note_Event).
Since the Note_Event struct is defined inside the EventQueue class, i cant define as return parameter Note_Event*.

Code: Select all

protected :
virtual void addNote(int time, int note, int velocity);
virtual int getEventCount();
virtual int getNextEventTime//	
virtual Note_Event* getNextEvent();//
The getNextEvent looks like this :

Code: Select all

Note_Event* EventQueue::getNextNote() {
	Note_Event *ev;
	ev = m_events.pop_front();
	return ev;

// more to be added
        
 };
c:\vstsdk\vstsdk2\source\vstxsynth2\event.cpp(45) : error C2039: 'getNextNote' : is not a member of 'EventQueue'
c:\vstsdk\vstsdk2\source\vstxsynth2\event.h(6) : see declaration of 'EventQueue'
c:\vstsdk\vstsdk2\source\vstxsynth2\event.cpp(47) : error C2065: 'm_events' : undeclared identifier
c:\vstsdk\vstsdk2\source\vstxsynth2\event.cpp(47) : error C2228: left of '.pop_front' must have class/struct/union type

So how can I do this ? I tryed to define the struct as public or static struct ... but than I get lot's of errors.

Post

Its sometimes better to define the list as a pointer, so that you don't have to include the <list> header in the header file for you class. You have to forward declare the list class though:

Code: Select all

namespace std {
	template<typename T> class allocator;
	template<typename T, class A = allocator<T> > class list;
}

class SomeClass
{
public:
	SomeClass();

private:
	typedef std::list<int> IntList;
	
	IntList *plist_;
};
This reduces dependencies, and in large projects can lead to drastically reduced compile times. It is fine to have partial classes (forward declared ones) as return-by-value types, or as pass-by-value types. In a real class, it is likely that you would want to declare a copy constructor and assignment operator to prevent wierd things happening.

Post Reply

Return to “DSP and Plugin Development”