Local class and static variables within a function

Hi, I would like to know if something of this sort is legal, standard c++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  template<typename value>
void Database<value>::select(DBSelectOperation selOp, const string&\
attr,DBQueryOperator op, const value& val){
	static string attr_stc;
	static DBQueryOperator op_stc;
	static value val_stc;
	attr_stc = attr;
	op_stc = op;
	val_stc = val;
	struct match_ptr{
		typedef Record<value>* argument_type;
		typedef bool return_type;
		return_type operator()(argument_type a) const{
			return a->matchesQuery(attr_stc,op_stc,val_stc);
		}
	};
......(rest of the function. match_ptr is only used in std::remove_if as the unary operator)


Visual Studio 2012 seems to work fine with it, while gcc complains that the static variables are unused; furthermore, when I set their attributes to "unused", just to disable the warnings (treated as error compulsorily), it seems the line that uses the variables,
 
    return a->matchesQuery(attr_stc,op_stc,val_stc);

produces an "undefined reference" error. So which one of the compilers is behaving according to c++ standard in this case? What is the cause of the undefined reference error and the variables being seen as "unused"?
Last edited on
wenqiwei wrote:
produces an "undefined reference" error.
Did you compile the source files when compiling with gcc, or did you use just the header and tried to make a complete program?
Did you compile the source files when compiling with gcc, or did you use just the header and tried to make a complete program?

I did compile the source file. These are the compile commands (unchangeable):

g++ -std=c++11 -Wall -Werror -O2 -c -o interactive.o interctive.cpp
g++ -std=c++11 -Wall -Werror -o db interactive.o fraction.o

where interactive.cpp is where the main() function is, and fraction.o is a pre-compiled object file. The "unused variable" warning is given by the first command, while the "undefined reference" error is given by the second.
Last edited on
If the object file was compiled for MSVC then you won't be able to use it with GCC.
If the object file was compiled for MSVC then you won't be able to use it with GCC.

The object file was compiled for gcc. It was given.
Then how are you compiling with MSVC?
Then how are you compiling with MSVC?

I first tried compiling all the source files with MSVC, without using any of the given object files compiled with GCC (I was given the source files of fraction.o as well), and the code compiled and ran without any issues; then I tried compiling on linux using GCC, and had the above issues.
If you have the source of fraction.o can you try compiling with that instead of the object file?

The issue is that object files are only good for the same system and same compiler with the same settings that they were compiled with - if anything is off then you get weird errors.
Agh..
I tried compiling the files together, and it does exactly the same thing X-(
the compiling command is:
g++ -std=c++11 -O2 -o db interactive.cpp fraction.cpp
And the compiler output is extremely long and nonsensical. Behold, here's an excerpt:


interactive.cpp:
(.text._ZSt9__find_ifIN9__gnu_cxx17__normal_iteratorIPP6RecordIiESt6vec
torIS4_SaIS4_EEEESt12unary_negateIZN8DatabaseIiE6selectE17DBSelect
OperationRKSs15DBQueryOperatorRKiE9match_ptrEET_SL_SL_T0_St26rand
om_access_iterator_tag[_ZSt9__find_ifIN9__gnu_cxx17__normal_iteratorI
PP6RecordIiESt6vectorIS4_SaIS4_EEEESt12unary_negateIZN8DatabaseIiE
6selectE17DBSelectOperationRKSs15DBQueryOperatorRKiE9match_ptrEET_
SL_SL_T0_St26random_access_iterator_tag]+0x31): undefined reference
to `op_stc'
interactive.cpp:
(.text._ZSt9__find_ifIN9__gnu_cxx17__normal_iteratorIPP6RecordIiESt6vec
torIS4_SaIS4_EEEESt12unary_negateIZN8DatabaseIiE6selectE17DBSelect
OperationRKSs15DBQueryOperatorRKiE9match_ptrEET_SL_SL_T0_St26rand
om_access_iterator_tag[_ZSt9__find_ifIN9__gnu_cxx17__normal_iteratorI
PP6RecordIiESt6vectorIS4_SaIS4_EEEESt12unary_negateIZN8DatabaseIiE
6selectE17DBSelectOperationRKSs15DBQueryOperatorRKiE9match_ptrEET_
SL_SL_T0_St26random_access_iterator_tag]+0x39): undefined reference
to `val_stc'

And so forth
Last edited on
If you don't use something the compiler won't try to find it, which is why you only get the error with certain combinations of code.
Topic archived. No new replies allowed.