我应该如何在我的 C++ 项目中声明全局变量?
How should I declare global variables in my C++ project?
我有两个矩阵作为全局变量。但是,当我 运行 我的项目时,我在 xCode 中收到 apache Mach-O 链接器错误,表明我的全局变量被声明了不止一次。我确定问题出在我的全局变量的位置和头文件的导入上。
我的 svd.h 在这里:
#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__) */
我的 svd.cpp 在这里:
#include "svd.h"
void initialize(int num_features) {
for(int i = 0; i < 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 i = 0; i < 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;
}
}
}
我的 main.cpp 看起来像这样:
#include <iostream>
#include "svd.h"
int main(int argc, const char * argv[]) {
// Parse file and store test points as testPoint objects
std::vector<testPoint*> dataSet = fillTestPoints();
// Get global average of data set
/*
double avg = getGlobalAverage(dataSet);
printf("%f", avg);
*/
initialize(30);
for(int i = 0; i < TOTAL_USERS; i++) {
printf("%f\n", user_feature_table[i][0]);
}
return 0;
}
我运行之前遇到过这个问题,但是通过删除全局变量解决了这个问题。但是,我确实需要优化此代码,而使用全局变量是实现此目的的方法,因此我确实需要弄清楚这一点。谢谢!
在头文件中,仅声明它们。
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;
在您的一个 .cpp 文件中,定义并初始化它们:
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];
我有两个矩阵作为全局变量。但是,当我 运行 我的项目时,我在 xCode 中收到 apache Mach-O 链接器错误,表明我的全局变量被声明了不止一次。我确定问题出在我的全局变量的位置和头文件的导入上。
我的 svd.h 在这里:
#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__) */
我的 svd.cpp 在这里:
#include "svd.h"
void initialize(int num_features) {
for(int i = 0; i < 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 i = 0; i < 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;
}
}
}
我的 main.cpp 看起来像这样:
#include <iostream>
#include "svd.h"
int main(int argc, const char * argv[]) {
// Parse file and store test points as testPoint objects
std::vector<testPoint*> dataSet = fillTestPoints();
// Get global average of data set
/*
double avg = getGlobalAverage(dataSet);
printf("%f", avg);
*/
initialize(30);
for(int i = 0; i < TOTAL_USERS; i++) {
printf("%f\n", user_feature_table[i][0]);
}
return 0;
}
我运行之前遇到过这个问题,但是通过删除全局变量解决了这个问题。但是,我确实需要优化此代码,而使用全局变量是实现此目的的方法,因此我确实需要弄清楚这一点。谢谢!
在头文件中,仅声明它们。
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;
在您的一个 .cpp 文件中,定义并初始化它们:
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];