即使代码正确,我的 Visual Studio 编译器也会向我显示错误消息。
即使是像这样的简单代码,
#include<stdio.h>
int main(){
int add(firstNumber, secondNumber){
int result=firstNumber+secondNumber;
return result;
}
int out=add(10,30);
printf("%d", out);
return 0;
}
它正在显示按摩,
<块引用>应为“;”,第 4 行
和
<块引用>标识符“out”未定义,第 9 行
代码运行良好。但是在我的代码中看到那些红线和错误消息令人沮丧。
注意:我使用 MinGW 作为 C 路径
答案 0 :(得分:3)
C 和 C++ 标准都不允许在这个程序中定义嵌套函数
function Set-DoubleBuffered{
<#
.SYNOPSIS
Turns on double buffering for a [System.Windows.Forms.Control] object
.DESCRIPTION
Uses the Non-Public method 'SetStyle' on the control to set the three
style flags recomend for double buffering:
UserPaint
AllPaintingInWmPaint
DoubleBuffer
.INPUTS
[System.Windows.Forms.Control]
.OUTPUTS
None
.COMPONENT
System.Windows.Forms.Control
.FUNCTIONALITY
Set Flag, DoubleBuffering, Graphics
.ROLE
WinForms Developer
.NOTES
Throws an exception when trying to double buffer a control on a terminal
server session becuase doing so will cause lots of data to be sent across
the line
.EXAMPLE
#A simple WinForm that uses double buffering to reduce flicker
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Pen = [System.Drawing.Pen]::new([System.Drawing.Color]::FromArgb(0xff000000),3)
$Form = New-Object System.Windows.Forms.Form
Set-DoubleBuffered $Form
$Form.Add_Paint({
param(
[object]$sender,
[System.Windows.Forms.PaintEventArgs]$e
)
[System.Windows.Forms.Form]$f = $sender
$g = $e.Graphics
$g.SmoothingMode = 'AntiAlias'
$g.DrawLine($Pen,0,0,$f.Width/2,$f.Height/2)
})
$Form.ShowDialog()
.LINK
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.setstyle?view=net-5.0
.LINK
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.controlstyles?view=net-5.0
#>
param(
[parameter(mandatory=$true,ValueFromPipeline=$true)]
[ValidateScript({$_ -is [System.Windows.Forms.Control]})]
#The WinForms control to set to double buffered
$Control,
[switch]
#Override double buffering on a terminal server session(not recomended)
$Force
)
begin{try{
if([System.Windows.Forms.SystemInformation]::TerminalServerSession -and !$Force){
throw 'Double buffering not set on terminal server session.'
}
$SetStyle = ([System.Windows.Forms.Control]).GetMethod('SetStyle',
[System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance
)
$UpdateStyles = ([System.Windows.Forms.Control]).GetMethod('UpdateStyles',
[System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance
)
}catch {$PSCmdlet.ThrowTerminatingError($PSItem)}
}process{try{
$SetStyle.Invoke($Control,@(
([System.Windows.Forms.ControlStyles]::UserPaint -bor
[System.Windows.Forms.ControlStyles]::AllPaintingInWmPaint -bor
[System.Windows.Forms.ControlStyles]::DoubleBuffer
),
$true
))
$UpdateStyles.Invoke($Control,@())
}catch {$PSCmdlet.ThrowTerminatingError($PSItem)}}
}
其中函数 #include<stdio.h>
int main(){
int add(firstNumber, secondNumber){
int result=firstNumber+secondNumber;
return result;
}
int out=add(10,30);
printf("%d", out);
return 0;
}
在函数 add
中定义。
此外,参数列表中的标识符 main
和 firstNumber
没有类型说明符。
您必须将函数定义移到 main 之外。例如
secondNumber
在 C++ 中,您可以使用 lambda 表达式而不是函数 #include<stdio.h>
int add( int firstNumber, int secondNumber )
{
int result = firstNumber + secondNumber;
return result;
}
int main( void ){
int out=add(10,30);
printf("%d", out);
return 0;
}
。例如
add
答案 1 :(得分:1)
问题是你在 main 中定义的函数,C 不支持在其他函数中定义的函数。您的代码应如下所示。
#include <stdio.h>
int add(firstNumber, secondNumber){
int result=firstNumber+secondNumber;
return result;
}
int main(){
int out=add(10,30);
printf("%d", out);
return 0;
}