【问题标题】:main.cpp:146:1: error: expected unqualified-id before ‘{’ token [closed]main.cpp:146:1:错误:“{”令牌之前的预期 unqualified-id [关闭]
【发布时间】:2021-03-16 19:24:31
【问题描述】:

我收到错误:“在 '{' 令牌之前应该是不合格的 ID”。据说这发生在 Thomas 算法函数中。我从未见过此错误,但我认为这是由于函数、原型、函数调用或变量声明的格式设置造成的。

此代码最终通过输入矩阵的对角元素 (l,d,u)(由用户输入)和另一个向量的元素 (b) 来执行 Thomas 算法。这是一个迭代循环代码,最终呈现向量 (x) 作为结果

#include <iostream>
#include <fstream>
#include <math.h>
#include <vector>
#define max_size 11
using namespace std;


//Function Prototype//

void ThomasAlgorithm(double l[max_size], double d[max_size], double u[max_size], double b[max_size], double X[max_size], int n);

//-----------------------------------------------------------------------------------------------------------//


//Main Code//

int main()
{

//Creating/Writing to Output File
FILE * (Thom_out);
Thom_out = fopen("ACT_HW4_Thomas.txt", "w");  
        
//Defining variables/matrices
int i,j,k,n;
double d[max_size], l[max_size], u[max_size];
double b[max_size], X[max_size]; //(I start at 1, not 0)

cout << "Define the system's initial matrices.\n";

//Entering Matrix Dimension
cout << "Enter the matrix dimension value (n): ";
cin >> n; 

//Matrix 1 (A) - Diagonal Elements (l,d,u)
n = n+1;
l[1] = 0;
u[n-1] = 0;

cout << "\nEnter matrix 1 - lower diagonal element (l):   \n";
cout << "Value l1 : 0\n";
for(int i=2; i<n; ++i)
{
    cout << "Value l" << i << " : ";
    cin >> l[i];
}

cout << "\nEnter matrix 1 - middle diagonal element (d):   \n";
for(int i=1; i<n; ++i)
{
    cout << "Value d" << i << " : ";
    cin >> d[i];
}

cout << "\nEnter matrix 1 - upper diagonal element (u):   \n";
for(int i=1; i<n-1; ++i)
{
    cout << "Value u" << i << " : ";
    cin >> u[i];
}
cout << "Value u" << n-1 << " : 0\n";

// Matrix 2 (B)
cout << "\nEnter matrix 2 values (B):   \n";
for(int i=1; i<n; ++i)
{
    cout << "Value B" << i << " : ";
    cin >> b[i];
}

//Printing Matrix Dimensions/Values to Output File
fprintf(Thom_out, "Matrix 1 - Lower Diagonal Element (l):   \n");

for (i=1; i<n; i++)
{
    fprintf (Thom_out, "%7.3f\t", l[i]);
    fprintf (Thom_out, "\n");
}

fprintf(Thom_out,"\n");  
fprintf(Thom_out, "Matrix 1 - Middle Diagonal Element (d):   \n");

for (i=1; i<n; i++)
{
    fprintf (Thom_out, "%7.3f\t", d[i]);
    fprintf (Thom_out, "\n");
}

fprintf(Thom_out,"\n");
fprintf(Thom_out, "Matrix 1 - Upper Diagonal Element (u):   \n");

for (i=1; i<n; i++)
{
    fprintf (Thom_out, "%7.3f\t", u[i]);
    fprintf (Thom_out, "\n");
}

fprintf(Thom_out,"\n");  
fprintf(Thom_out, "Matrix 2 (B):   \n");

for (i=1; i<n; i++)
{
    fprintf (Thom_out, "%7.3f\t", b[i]);
    fprintf (Thom_out, "\n");
}

fprintf(Thom_out,"\n");

//Call Thomas Algorithm Function
ThomasAlgorithm(l,d,u,b,X,n);

//Printing Thomas Result to Output File
fprintf(Thom_out, "Thomas Algorithm Resultant (X):   \n");
for (i=1; i<n; i++)
{
    fprintf (Thom_out, "%7.3f\t", X[i]);
    fprintf (Thom_out, "\n");
}

return 0;
}

//-----------------------------------------------------------------------------------------------------------//


//Thomas Algorithm Function//

void ThomasAlgorithm(double l[max_size], double d[max_size], double u[max_size], double b[max_size], double X[max_size], int n);
{

//Defining Function Variables    
int i, j, k;

//Thomas Algorithm Looping Code
for(i=2; i<n; i++)
{
    d[i] = d[i] - (l[i]/d[i-1])*u[i-1];
    b[i] = b[i] - (l[i]/d[i-1])*b[i-1];
}

x[n] = b[n]/d[n];

for(k=1; k<n-1; k++)
{
    i = n - k;
    x[i] = (b[i] - u[i]*x[i+1])/d[i]
}

return;
}

//-----------------------------------------------------------------------------------------------------------//


【问题讨论】:

  • { 标记之前的“意外”字符是错误的;。删除它。

标签: c++ function token


【解决方案1】:

第 129 行,你有一个 ; {

之前
void ThomasAlgorithm(double l[max_size], double d[max_size], double u[max_size], double b[max_size], double X[max_size], int n);
{

你也没有声明 x,只有 X 并且你忘记了 a ;在第 147 行

【讨论】:

  • 谢谢!我正在为一个简单的答案而努力。所有的 x 都应该是 X
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2014-03-07
  • 2011-01-15
相关资源
最近更新 更多