我的软件有一个主要用于正常使用,另一个用于单元测试.如果gcc有一个选项来指定使用哪个"主要"功能,我会喜欢它.
由于C+++允许函数重载,我们可以重载main()吗?
例如,
int main(const std::string &)
{
return 0;
}
int main(int argc, char *argv[])
{
return main("calling overloaded main");
}
Run Code Online (Sandbox Code Playgroud)
gcc-4.3.4不编译这个,并给出这些错误:(见ideone)
prog.cpp:4:错误:'int main(const std :: string&)'的第一个参数应该是'
int'prog.cpp:4:错误:'int main(const std :: string&)'只取零或两个参数
prog.cpp:在函数'int main(int,char**)'中:
prog.cpp:8:错误:C函数的声明'int main(int,char**)'与
prog.cpp 冲突:4 :错误:上一个声明'int main(const std :: string&)'这里
prog.cpp:在函数'int main(int,char**)'中:
prog.cpp:10:错误:从'const char*转换无效'
to'int'prog.cpp:8:错误:函数'int main(int,char**)'
prog.cpp的参数太少:10:错误:此时在文件中
所以我想知道C++标准是否明确禁止重载main?如果是这样,哪个陈述?
因此,如果我使用eclipse CDT调试我的C++代码,它似乎总是从main()函数启动调试过程,即使在main()开头没有断点...
有没有办法让eclipse CDT从第一个断点而不是main()开始调试?
导致此错误的原因是什么?我google了它,我发现的前几个解决方案是库和主要功能有问题,但在我的问题中似乎都很好,我甚至重新输入了两个!可能是什么导致了这个?
这可能会有所帮助:
MSVCRTD.lib(crtexew.obj):错误LNK2019:函数_ _tmainCRTStartup中引用的未解析的外部符号WinMain @ 16
#include <iostream>
using namespace std;
int main()
{
const double A = 15.0,
B = 12.0,
C = 9.0;
double aTotal, bTotal, cTotal, total;
int numSold;
cout << "Enter The Number of Class A Tickets Sold: ";
cin >> numSold;
aTotal = numSold * A;
cout << "Enter The Number of Class B Tickets Sold: ";
cin >> numSold;
bTotal = numSold * B;
cout << "Enter The Number of …Run Code Online (Sandbox Code Playgroud) 情况1:
void hello(void) {
//something
}
int main()
{
hello(1); //error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
案例2:
int main(void) {
//something
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行:
./a.out something something //No error, Why?
Run Code Online (Sandbox Code Playgroud)
为什么没有错误?main将无法采取任何论点.那么为什么可以从命令行提供参数呢?
我唯一接触过编程的是Java,在那里我没有遇到(到现在为止)编写main方法的不同惯例.我一直在学习c(K&R和C编程现代方法)的来源,他们使用的非常不同形式的主要方法(功能).
K&R版本到现在为止:
main() {
blah blah blah;
}
Run Code Online (Sandbox Code Playgroud)
C编程现代方法
int main() {
blah blah blah;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
要么
int main() {
blah blah blah;
//returns nothing
}
Run Code Online (Sandbox Code Playgroud)
为了让事情更加混乱,我见过人们这样做:
int main(void) {
blah blah blah;
}
Run Code Online (Sandbox Code Playgroud)
虽然他们要么返回0,要么没有.我没有在我没有受过教育的假设中认为这只是一个标准问题,但可能更具概念性或更深层次.有人能解释一下这个问题吗?
我写了一个简单的模块叫做Test:
module Test (main) where
main =
putStrLn "Hello"
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试通过以下命令行编译它时:
ghc Test.hs -o my-program
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
[1 of 1] Compiling Test ( Test.hs, Test.o )
<no location info>: error:
output was redirected with -o, but no output will be generated because there is no Main module.
Run Code Online (Sandbox Code Playgroud) 我一直#[tokio::main]在我的一个程序中使用该宏。不合格的导入使用后main,遇到了意想不到的错误。
use tokio::main;
#[main]
async fn main() {}
Run Code Online (Sandbox Code Playgroud)
error[E0659]: `main` is ambiguous
--> src/main.rs:3:3
|
3 | #[main]
| ^^^^ ambiguous name
|
= note: ambiguous because of a name conflict with a builtin attribute
= note: `main` could refer to a built-in attribute
Run Code Online (Sandbox Code Playgroud)
我一直在搜索文档,但找不到#[main]任何地方描述的这个内置属性。Rust 参考包含内置属性的索引。该索引不包含#[main],但它包含名为 的属性#[no_main]。
我对存储库进行了搜索rustlang/rust,发现了一些似乎相关的代码,但它似乎使用了一对名为#[start]和 的宏#[rustc_main],而没有提及其#[main]本身。(这两个宏似乎都不能在稳定版上使用,会#[start]发出一个错误,表明它不稳定,并#[rustc_main]发出一个错误,表明它仅供编译器内部使用。)
我从名称中猜测它是为了将不同的函数标记为入口点而不是main,但它似乎也没有这样做:
error[E0659]: …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的 Go 项目设置。在根目录中,我有go.mod文件和main.go一个名为main2. 文件夹里面main2有main2.go文件。
/
|_ go.mod
|_ main.go
|_ main2
|_ main2.go
Run Code Online (Sandbox Code Playgroud)
从根目录我尝试运行 go run 命令
go run main2/main2.go
Run Code Online (Sandbox Code Playgroud)
它会抛出错误:
包命令行参数不是主包
有人可以帮忙吗?
c++ ×4
c ×3
entry-point ×2
debugging ×1
eclipse ×1
eclipse-cdt ×1
fatal-error ×1
gcc ×1
go ×1
go-toolchain ×1
haskell ×1
input ×1
overloading ×1
rust ×1
rust-macros ×1
standards ×1