命名空间函数干扰打印
我是该网站的新手,浏览了几个听起来类似的问题,但还没有找到我的确切问题。我怀疑我犯了低级错误。如果我的问题与帖子的标题不匹配,我将再次表示抱歉。
对于适用于 Android 的 Cxxdroid 应用程序和 Visual Studio C++ 中的 c++:
我正在尝试使用类和命名空间来为类提供灵活的实用程序。我想使用同一个函数的不同实现来对数据结构上的某些算法进行个人分析;即数组与列表/树以及标准过程的递归和迭代实现之间。我知道如何进行渐近分析,但我顽固的头脑想要实数。
然而不幸的是,我似乎甚至无法在不破坏正常功能的情况下使命名空间函数正常工作。请注意,我还没有正式学习 C++,因为我正在 C/C++ 入门课程之前进行探索。
例如:
#include <iostream>
namespace iterative{
int power(int base,int expo){...}
}
namespace recursive{
int power(int base,int expo){...}
}
int main(int argc,char* argv[]){
int result = 0;
int num = 3;
int exp = 2;
//Expecting 9 in result
std::cout << "This prints fine" << std::endl;
result = iterative::power(num,exp);
std::cout << result;
std::cout << " This number and text doesn't print" << std::endl;
return 1;
}
我有一个更复杂的函数,其类位于命名空间函数之上(列表/树类中节点类的搜索函数),但它从未起作用。然后我只是将上面的代码片段放在一起并尝试打印结果,但 std::cout 在我的函数调用后从未触发。
谁能深入了解我在这里做错了什么?第一个 cout 打印,第二个则不打印。此外,在函数调用期间执行会挂起;该解决方案一直运行,直到我强制它停止。
我尝试在使用另一个名称空间时注释掉其中一个名称空间。
我已经尝试过使用关键字与namespace_name。
我尝试过传递整数而不使用变量: result = power(3,2);
我尝试直接打印函数结果: std::cout <<幂(3,2) << std::endl;
我似乎无法让它在任何一个应用程序上工作。我知道这似乎是一个愚蠢而简单的问题,但在浏览互联网大约一周后,我被有关语法的问题的非常模糊的答案所淹没。也许我只是没有将这些点与我自己的问题联系起来......
我的定义语法是否错误? 我调用函数的语法是否错误? 我的变量赋值语法是否错误?
我已经无计可施了。
编辑:
现在我觉得自己真的很愚蠢。
你是对的。我没有在迭代实现中增加变量,这意味着它挂在无限循环上。我必须打印循环才能看到数字吐出,就像我是《黑客帝国》中的尼奥一样。不幸的是,我没有测试递归函数,因为如果我什至无法首先调用迭代函数,那么这样做感觉很危险......
我第一次专注于使用命名空间,以至于我从未看过正确循环。
谢谢您,抱歉打扰了。我现在将尝试将此实验扩展到命名空间定义的类成员函数。
Edit2:随意删除这个......除非我觉得我的愚蠢可以帮助其他人。
I'm new to the site and browsed several similar sounding questions but haven't found my exact problem. I suspect I'm making elementary mistakes. A link to an answer would be appreciated, or an explanation, and again I'm sorry if my question doesn't even match the title of the post.
For c++ on the Cxxdroid app for android and in Visual Studio C++:
I'm trying to experiment with classes and namespaces to provide flexible utility to the class. I want to use different implementations of the same function for personal analysis of certain algorithms on data structures; namely arrays vs lists/trees and also between recursive and iterative implementations of standard procedures. I know how to do asymptotic analysis but my stubborn mind wants real numbers.
Unfortunately, however, I can't even seem to get namespace functions to work without blowing up normal functionality. Please note, I haven't learned c++ formally yet because I'm exploring ahead of my introductory c/c++ course.
For example:
#include <iostream>
namespace iterative{
int power(int base,int expo){...}
}
namespace recursive{
int power(int base,int expo){...}
}
int main(int argc,char* argv[]){
int result = 0;
int num = 3;
int exp = 2;
//Expecting 9 in result
std::cout << "This prints fine" << std::endl;
result = iterative::power(num,exp);
std::cout << result;
std::cout << " This number and text doesn't print" << std::endl;
return 1;
}
I had a much more complex function with classes above the namespaced functions (search function for node classes inside a list/tree class) but it never worked. Then I just threw together the above snippet and tried to print result but the std::cout never fired after my function call.
Can anyone offer insight into what I'm doing wrong here? The first cout prints and the second wont. Further, execution hangs during the function call; the solution keeps running until I force it to stop.
I've tried to comment out one of the namespaces while using the other.
I've tried the using keyword with the namespace_name.
I've tried passing integers without variable usage: result = power(3,2);
I've tried printing the function result directly: std::cout << power(3,2) << std::endl;
I can't seem to get it to work on either application. I know this seems like a silly and simple question but after about a week of browsing the internet I'm inundated with very vague answers to questions regarding syntax. Perhaps I'm just not connecting the dots to my own problem...
Is my syntax in definition wrong?
Is my syntax in calling the function wrong?
Is my syntax in variable assignment wrong?
I'm at my wits' end.
Edit:
Now I feel really stupid.
You were correct. I didn't increment my variable in the iterative implementation, which meant it hung up on an infinite loop. I had to print the loop to see the numbers spitting out like I'm Neo in The Matrix. Unfortunately, I didn't test the recursive function because it felt dangerous to do so if I couldn't even get the iterative function to call first...
I was so focused on using namespaces for the first time that I never looked at the loop properly.
Thank you, and sorry for the bother. I'm going to try to extend this experiment to namespace-defining class member functions now.
Edit2: Feel free to delete this...unless it's felt that my stupidity can help others.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
犯了愚蠢的错误:
Silly mistake was made: