涉及数组子类型的多个错误

时间:2016-02-22 05:25:15

标签: c++

#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <thread>


using namespace std;


float writethread1(float storageptr, uint64_t storageexpptr, uint64_t num3, uint64_t counter, uint64_t exp){
//Printing the first half of numbers that the user requested.
counter = 0;
exp = 0;
exp = storageexpptr[counter];
//Printing numbers that do have e<3 at the end.
while (counter < 0.5 * num3){
    if (exp < 3){
        ofstream fileout;
        fileout.open ("fibonacci.txt", ios_base::app);
        fileout << fixed << "#" << counter << ": " << storageptr[counter] << "\n";
        counter += 1;
    }
//Printing numbers that do have e3> at the end.
    else{
        ofstream fileout;
        fileout.open ("fibonacci.txt", ios_base::app);
        fileout << fixed << "#" <<counter<< ": " << storageptr[counter] << "e" << storageptr[counter] << "\n";
        counter+=1;
    }
}
}

float writethread2(float storageptr, uint64_t storageexpptr, uint64_t num3, uint64_t counter, uint64_t exp){
//Printing the second half of numbers that the user requested.
counter = 0.5 * num3 +1;
exp = storageptr[counter];
//Printing numbers that do have e<3 at the end.
while(counter < num3){
    if (exp < 3){
        ofstream fileout;
        fileout.open ("fibonacci.txt", ios_base::app);
        fileout << fixed << "#" << counter << ": " << storageptr[counter] << "\n";
        counter += 1;
    }
//Printing numbers that do have e3> at the end.
    else{
        ofstream fileout;
        fileout.open ("fibonacci.txt", ios_base::app);
        fileout << fixed << "#" <<counter<< ": " << storageptr[counter] << "e" << storageexpptr[counter] << "\n" ;
        counter+=1;
    }
}
}

int main(){
//Declaring some variables.
float *storageptr;
uint64_t *storageexpptr;
float num1 = 0;
float num2 = 1;
uint64_t num3;
uint64_t counter = 1;
uint64_t exp = 0;
//User input to determine how many numbers the program generates.
cout << "How many numbers do you want to generate?" << endl;
cin >> num3;
vector <uint64_t> storageexp(num3);
vector <float> storage(num3);
storageexpptr = &storageexp;
storageptr = &storage;
//Calculator
while (counter<num3) {
    if (counter %2 == 0) {
        num1+=num2;
        storage[counter] = num1;
        storageexp[counter] =exp;
        counter+=1;
    }
    else{
        num2+=num1;
        storage[counter] = num2;
        storageexp[counter] =exp;
        counter+=1;
    }
    if( num1>1000) {
        num1/=1000;
        num2/=1000;
        exp+=2;
    }
    else if(num2>1000) {
              num1=num1/1000;
              num2=num2/1000;
              exp+=3;
    }

}

cout << "Calculation complete. Writing to file." << endl;
//Print to file preperations.
//Spawning a couple of threads.
thread thread1 (writethread1);
thread thread2 (writethread2);

}

编译器输出:

main.cpp||In function 'float writethread1(float, uint64_t, uint64_t, uint64_t, uint64_t)':|
main.cpp|15|error: invalid types 'uint64_t {aka long long unsigned int}[uint64_t {aka long long unsigned int}]' for array subscript|
main.cpp|21|error: invalid types 'float[uint64_t {aka long long unsigned int}]' for array subscript|
main.cpp|28|error: invalid types 'float[uint64_t {aka long long unsigned int}]' for array subscript|
main.cpp|28|error: invalid types 'float[uint64_t {aka long long unsigned int}]' for array subscript|
main.cpp||In function 'float writethread2(float, uint64_t, uint64_t, uint64_t, uint64_t)':|
main.cpp|37|error: invalid types 'float[uint64_t {aka long long unsigned int}]' for array subscript|
main.cpp|43|error: invalid types 'float[uint64_t {aka long long unsigned int}]' for array subscript|
main.cpp|50|error: invalid types 'float[uint64_t {aka long long unsigned int}]' for array subscript|
main.cpp|50|error: invalid types 'uint64_t {aka long long unsigned int}[uint64_t {aka long long unsigned int}]' for array subscript|
main.cpp||In function 'int main()':|
main.cpp|70|error: cannot convert 'std::vector<long long unsigned int>*' to 'uint64_t* {aka long long unsigned int*}' in assignment|
main.cpp|71|error: cannot convert 'std::vector<float>*' to 'float*' in assignment|

该代码的目标是打印出2个数组的值,其值在main函数中确定。我试图使用指针来引用数组并使用指针拉取它们的值。编译抱怨,从来没有与它相处。基本上,我不了解编译器给我的信息。任何帮助解释它将不胜感激。或者,如果您有解决方案,请提供相应的信息,以及导致您得出结论的原因。

建议更改后的编译器输出:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\functional||In instantiation of 'struct std::_Bind_simple<void (*())(float*, long long unsigned int*, long long unsigned int, long long unsigned int, long long unsigned int)>':|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\thread|137|required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(float*, long long unsigned int*, long long unsigned int, long long unsigned int, long long unsigned int); _Args = {}]'| 
FIBONACCI\main.cpp|103|required from here|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\functional|1665|error: no type named 'type' in 'class std::result_of<void (*())(float*, long long unsigned int*, long long unsigned int, long long unsigned int, long long unsigned int)>'|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\functional|1695|error: no type named 'type' in 'class std::result_of<void (*())(float*, long long unsigned int*, long long unsigned int, long long unsigned int, long long unsigned int)>'|
||=== Build failed: 2 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|

1 个答案:

答案 0 :(得分:1)

你反复使用float和uint64_t参数作为第15,21,28,37,43,50行的数组。我不能确定,但​​根据main()中调用参数的定义,我您认为您希望将writethreadN函数的相应参数看起来像:

float writethread1(float* storageptr, uint64_t* storageexpptr, uint64_t num3, uint64_t counter, uint64_t exp){

此外,您没有在这些函数中返回值,即使它们的返回类型为float而不是void,某些编译器可能不喜欢(在修复参数之后)。

最后,在第70和71行,你试图获取一个向量的地址并将它指定给一个指针,当你可能想做这样的事情时:

storageexpptr = storageexp.data();
storageptr = storage.data();

产生线程的代码看起来并不正确,因为你指定了线程处理函数,每个函数需要五个参数,但是在创建实际的线程对象时没有指定那些参数。

当线程完成时,你的main()中是否需要等待循环?我现在用这么多语言工作,我无法跟踪哪些需要,哪些不需要,但我想提一下,以防你解决所有问题而没有任何反应。