Change class member data in a global function

I am having a problem changing the value in line 47. I can't find the documentation that shows how to change the data. Eventually I will be moving this to the CSector.h class. I am just testing to see if the desired action takes place in the sector class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include "CSpecies.h"
#include "CItem.h"
#include "CShip.h"
#include "CPlayer.h"
#include "CAlien.h"
#include "CAmmo.h"
#include "CArmor.h"
#include "CBeam.h"
#include "CCargo_Hold.h"
#include "CComputer.h"
#include "CEngine.h"
#include "CHull.h"
#include "CMissile.h"
#include "CProjectile.h"
#include "CPwr_Gen.h"
#include "CSector.h"
#include "CSensor.h"
#include "CShield_Gen.h"
#include "CWeapon.h"


using namespace std;


void movetest(string move);//test function

int main()
{
    //test data start
    CShip a;
    CSector s0001;//variables have to start with a letter or _
    CSector s0002;
    s0001.sete_north(true);
    s0001.setnorth_ID(0002);
    s0001.sete_south(false);
    s0002.sete_south(true);
    s0002.setsouth_ID(0001);
    s0002.sete_north(false);
    //test data ends
    return 0;
}

void movetest(string move)//test function
{
    string newpos = move;
    if(move == "n"||"north" && s0001.gete_north())
    {
            a.setloc(s0001.getnorth_ID());
        }
}


The compiler errors say:

C:\Users\jason and veronica\Desktop\Dashboards\Projects\Enemy Mine\main.cpp||In function 'void movetest(std::string)':|

C:\Users\jason and veronica\Desktop\Dashboards\Projects\Enemy Mine\main.cpp|47|error: 's0001' was not declared in this scope|

C:\Users\jason and veronica\Desktop\Dashboards\Projects\Enemy Mine\main.cpp|49|error: 'a' was not declared in this scope|


I don't recall having this problem before. But I suspect I will have to use pointers to do the deed. I am having such problems with pointers, even with all the tutorials I am reading about them.

Some insite will be appreciated. Thanks ahead. :o)
You need to pass s0001 into your function, as a pointer or otherwise, or my personal suggestion would be to add this function to your 'CSector' class as member function.
Last edited on
Thank you computergeek01. I was planning to do that. I don't know why I didn't just do that in the first place. And it was just gonna be a simple test. It became more complicated though. Time to pop the function in. I guess I just needed a little push. It sure would solve a few problems and it needs to go in anyway. Thanks again. But could you tell me how to pass s0001 as a pointer? It would help a lot in the rest of the program.
void movetest(std::string, CSector*); <- Prototype.

1
2
void movetest(std::string move, CSector* s0001)
{/*Code Code Code*/}
^ Function Header
Last edited on
When I do that would this work?:
1
2
3
4
void movetest(std::string move, CSector *s0001)
{
    a.setloc(s0001->getnorth_ID());
}


Or would I need to do this?:
1
2
3
4
void movetest(std::string move, CSector *s0001)
{
    a.setloc(&s0001->getnorth_ID());
}


If this seems crazy it's probably because I'm typing this code laaaate at night. Thanks again. :o)
The first one should work. Try it, it takes how many seconds to add a character, save and recompile the app?
Well, the only reason I ask is so you can slap me with the logic as to why one or the other would be a serious miscalculation in judgement. (Or something like that.) I am still having problems with pointers and references.
Topic archived. No new replies allowed.