C++: scope, different files etc..


 
Thread Tools Search this Thread
Top Forums Programming C++: scope, different files etc..
# 1  
Old 04-24-2002
C++: scope, different files etc..

I'm having a problem getting this to work..
I got 3 files,
start.C - Where i got my main() function
Menu.C & Menu.h - Where I'm trying to use hash_map


start.C
#include <iostream>
#include "Menu.h"
using namespace std;

int main() { /* test code here */ return 0; }



Menu.h
#ifndef _MENU_H_
#define _MENU_H_
class Menu {
public:
Menu() { }
~Menu() { }

private:
hash_map< const char*, int, hash<const char*>, eqstr > hashmap;
};
#endif




Menu.C
#include <iostream>
#include <string>
#include <hash_map>

struct eqstr {
bool operator()(int s1, int s2) {
return s1 == s2;
}
};

#include "Menu.h"




-----------------------------
The code doesn't actually do anything now but I just want to get this working before i continue my little project. I've tried using hash_map in a separate file and it worked.

Compiler errors for the code above:

[jp@Slacktop:~/devel/cpp/001/] g++ start.C Menu.C

In file included from start.C:2:
Menu.h:10: `hash' was not declared in this scope
Menu.h:10: parse error before `char'
Menu.h: In method `Menu::Menu()':
Menu.h:6: parse error before character 0240
Menu.h: In method `Menu::~Menu()':
Menu.h:7: parse error before character 0240
In file included from Menu.C:11:
Menu.h: In method `Menu::Menu()':
Menu.h:6: parse error before character 0240
Menu.h: In method `Menu::~Menu()':
Menu.h:7: parse error before character 0240

The second line "'hash' was not declared in this scope" is probably the root of this evil error but I'm clueless on how to fix this..
any suggestions?

guidelines and recommendations on how to use multiple files are also appreciated.

Smilie


Slackware 8.0, Linux 2.4.5, G++ 2.95.3

Last edited by J.P; 04-24-2002 at 04:45 PM..
# 2  
Old 04-25-2002
Lightbulb solved it!

Menu.C
#include <iostream>
#include <string>
#include "Menu.h"

/* Coooooooooode */



Menu.h
#ifndef _MENU_H_
#define _MENU_H_
#include <hash_map>

struct eqstr {
bool operator()(int s1, int s2) {
return s1 == s2;
}
};

class Menu {
public:
Menu() { }
~Menu() { }

private:
hash_map< const char*, int, hash<const char*>, eqstr > hashmap;
};
#endif
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable scope in bash

Hello! Before you "bash" me with - Not another post of this kind Please read on and you will understand my problem... I am using the below to extract a sum of the diskIO on a Solaris server. #!/bin/sh PATH=/usr/bin:/usr/sbin:/sbin; export PATH TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
Discussion started by: haaru
3 Replies

2. Shell Programming and Scripting

Help with retaining variable scope

Hi, I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count. while read name do Tmp=`echo $name | awk '{print $9 }'` Count=`cat $Tmp | wc -l`... (6 Replies)
Discussion started by: justchill
6 Replies

3. Shell Programming and Scripting

bc: scope doesn't work for me

I am trying to use bc to calculate the difference between two nano second time stamps. bc does the calculation but seems to ignore the scale option: micro_start=$(date +%s.%N) # .. some stuff happens here micro_stop=$(date +%s.%N) TOT=$(echo "scale=3; $micro_stop - $micro_start" | bc)... (2 Replies)
Discussion started by: LostInTheWoods
2 Replies

4. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 Replies

5. Shell Programming and Scripting

scope of the variable - Naga

Hi All, I am new to unix shell scripting, in the below script "num" is an input file which contains a series of numbers example : 2 3 5 8 I want to add the above all numbers and want the result finally outside the while loop. it prints the value zero instead of the actual expected... (13 Replies)
Discussion started by: nagnatar
13 Replies

6. Shell Programming and Scripting

Variables scope.

Hi , I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file. #!/bin/sh FLAG=0; cat filename | while read data do FLAG=1; done echo $FLAG Should display 1 instead displays 0 (13 Replies)
Discussion started by: dinjo_jo
13 Replies

7. Shell Programming and Scripting

Doubt about variables scope

I call my script with two parameters myscript.sh aaa bbb What is the way to access $1 and $2 values inside a function? I call the function like this myfuntion $1 $1 but inside of the function, $1 and $2 are empty. Any suggestions? thank you in advanced. (1 Reply)
Discussion started by: aristegui
1 Replies

8. AIX

Scope of AIX

What is the scope of AIX as I am starting my career as a fresher in AIX administration?? (4 Replies)
Discussion started by: abhishek27
4 Replies

9. Programming

scope

Each thread has a copy of auto variables within a function, but variables declared as static within a function are common to all threads. To circumvent this can static variables be placed outside the function. If so, will the scope of the variable be file only or will it be extern, and will each... (7 Replies)
Discussion started by: sundaresh
7 Replies

10. Programming

C++ variable scope and mutexes

I've been wondering if I can make mutexes much easier to use in C++ with creative use of a locking class and variable scope, but I'm not sure if things happen in the order I want. Here's pseudocode for something that could use the class: int someclass::getvalue() { int retval; ... (0 Replies)
Discussion started by: Corona688
0 Replies
Login or Register to Ask a Question