【发布时间】:2018-01-29 07:04:12
【问题描述】:
我有一个小问题让我发疯,但对你们来说可能是小菜一碟。我定义了一个由 4 个数字组成的结构,并且我有一个返回该结构向量的函数。我的头文件如下所示:
Legendre.hpp
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector> //as suggested, but it doesn't help
#ifndef LEGENDRE_HPP_
#define LEGENDRE_HPP_
struct FourDoubles {
double Zeroth;
double First;
double Second;
double Third;
};
std::vector<FourDoubles> LegendreLookupTable(int size);
#endif /* LEGENDRE_HPP_ */
(其实还有一些功能,所以忽略那些看似毫无意义的包含) 然后我有以下使用标头的 cpp 文件:
Legendre.cpp
#include "Legendre.hpp"
using namespace std;
using namespace cv;
vector<FourDoubles> LegendreLookupTable(int size) {
vector<FourDoubles> res(size);
int i;
double x,x2,x3,x4;
for(i=0;i<size;i++) {
x=2.0*i/(size-1.0)-1.0;
x2=x*x;
x3=x2*x;
x4=x3*x;
res[i].Zeroth=x;
res[i].First=1.5*x2-0.5;
res[i].Second=2.5*x3-1.5*x;
res[i].Third=4.375*x4-3.75*x2+0.375;
}
return res;
}
以防万一有人好奇:我正在计算图像的勒让德时刻。但是,当我想编译它时,编译器告诉我“无法解析字段'Zeroth'”,其他字段也是如此。
当我注释掉文件中的所有其他内容并且我的主文件看起来像这样时,该错误已经发生
#include "Legendre.hpp"
using namespace std;
int main(int argc, char* argv[])
{
}
我得到的错误很简单 D
escription Resource Path Location Type
Field 'Third' could not be resolved Legendre.cpp /Finder line 108 Semantic Error
Field 'Second' could not be resolved Legendre.cpp /Finder line 107 Semantic Error
Field 'Zeroth' could not be resolved Legendre.cpp /Finder line 105 Semantic Error
Field 'First' could not be resolved Legendre.cpp /Finder line 106 Semantic Error
(别介意台词,我说过,我注释掉了很多东西)
有两件事对我来说很奇怪:如果我不声明一个向量,而是一个 FourDoubles 直接喜欢
vector<FourDoubles> LegendreLookupTable(int size) {
...
FourDoubles test;
test.Zeroth=5.5;
...
}
然后它工作得很好。另外,当我直接在我的主文件中编写整个内容时,例如
struct FourDoubles {
double Zeroth;
double First;
double Second;
double Third;
};
std::vector<FourDoubles> LegendreLookupTable(int size);
int main() {
...
}
vector<FourDoubles> LegendreLookupTable(int size) {
...
}
我可能在这里遗漏了一些非常基本的东西,但我只是不明白为什么它不能处理我的结构的向量,除非它在主文件中声明。任何启发的帮助将不胜感激。
【问题讨论】:
-
您是否加入了
vector? -
请尝试创建一个Minimal, Complete, and Verifiable Example 并向我们展示。并复制粘贴(作为文本)完整且完整(未经修改)的错误输出,包括可能的信息说明。
-
对不起,我会编辑一些澄清,给我一分钟。但是,事实上,我已经发布了一个完整的示例,因为在编译时已经出现错误。我只是注释掉了除了结构和函数之外的所有内容,即使我的主文件现在也是空的。但我会添加错误输出。