How should I declare global variables in my C++ project? -


i have 2 matrices global variables. however, when run project, apache mach-o linker error in xcode says global variables declared more once. i've determined problem placement of global variables , imports of header files.

my svd.h here:

#ifndef __netflix_project__svd__ #define __netflix_project__svd__  #include <stdio.h> #include "datamanager.h"  const float global_avg_set1 = 3.608609; const float global_avg_set2 = 3.608859;  const int total_users = 458293; const int total_movies = 17770;  double **user_feature_table = new double *[total_users]; double **movie_feature_table = new double *[total_movies];   void initialize(int num_features); void train(); double predictrating(int user, int movie);      #endif /* defined(__netflix_project__svd__) */ 

my svd.cpp here:

#include "svd.h"    void initialize(int num_features) {      for(int = 0; < total_users; i++) {          user_feature_table[i] = new double[num_features];          for(int k = 0; k < num_features; k++) {             user_feature_table[i][k] = global_avg_set2 / num_features;         }     }      for(int = 0; < total_movies; i++) {          movie_feature_table[i] = new double[num_features];          for(int k = 0; k < num_features; k++) {             movie_feature_table[i][k] = global_avg_set2 / num_features;         }     } } 

my main.cpp looks this:

#include <iostream> #include "svd.h"       int main(int argc, const char * argv[]) {      // parse file , store test points testpoint objects     std::vector<testpoint*> dataset = filltestpoints();       // global average of data set      /*     double avg = getglobalaverage(dataset);     printf("%f", avg);      */     initialize(30);      for(int = 0; < total_users; i++) {         printf("%f\n", user_feature_table[i][0]);     }      return 0; } 

i ran problem before, fixed taking out global variables. however, need optimize code, , using global variables way need figure out. thanks!

in header file, declare them.

extern const float global_avg_set1; extern const float global_avg_set2;  extern const int total_users; extern const int total_movies;  extern double **user_feature_table; extern double **movie_feature_table; 

in 1 of .cpp files, define , initialize them:

const float global_avg_set1 = 3.608609; const float global_avg_set2 = 3.608859;  const int total_users = 458293; const int total_movies = 17770;  double **user_feature_table = new double *[total_users]; double **movie_feature_table = new double *[total_movies]; 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -