d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Scope Problems With Classes
Add Reply New Topic New Poll
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
May 13 2013 10:54pm
NOTE: This is being written in the .cpp of Class3

Code
Class1 getSequence(Class2 organism){
     for (int i = 0; i < 100; i++){
          if (org[i].cname == organism.cname){
              return ds[i].dnaSeq;
          }
     }
}


both ds[] and org[] are in the header file of Class3 so I keep getting the "was not declared in this scope"...
In order to fix this, I'd have to do Class3::getSequence(Class2 organism), but I HAVE to keep it as Class1 getSequence(Class2 organism)
Is there any way to make this work?

I wanted to do this:

Code
Class1 getSequence(Class2 organism){
     for (int i = 0; i < 100; i++){
          if (Class3::org[i].cname == organism.cname){
              return Class3::ds[i].dnaSeq;
          }
     }
}


But here it says "invalid use of non-static data member 'Class3::ds', so what can I do here?
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 13 2013 11:12pm
Sounds like you're getting some concepts mixed up here.

The method signature
Code
Class1 getSequence(Class2 organism)

means that your method will return an object instance of type Class1.

The call
Code
Class3::getSequence(Class2 organism)

is a method inside the class, Class3, which presumably returns *something* (you haven't actually told us what it is here.. but I'm going to assume that it needs to return an object of type Class1, as you noted above).

Last but not least, non-static data/methods are meant to be called from a specific instance of a class. In this case, you're trying to access the array ds[] statically, when it's not a static data variable.

This post was edited by irimi on May 13 2013 11:13pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 13 2013 11:16pm
Anyway, with this and with scoping problems in general, you're going to need to post *all* the relevant before people can help you. In most cases, just a snippet will not tell anybody anything.
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
May 13 2013 11:38pm
Well I found a solution, but have another problem. I'll make another thread since this is irrelevant now
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll