Thread: Compile-time error

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    Compile-time error

    I can't get this code to compile in Microsoft Visual Studio 2013(it compiled in code blocks).
    I only need to know where the error is.
    Please help
    Code:
    #include"stdafx.h"
    #include<iostream>
    using namespace std;
    const long long limit = 1000001;
    long long cache[limit] = { 0 };
    inline long long next(long long n)
    {
    	if (n % 2 == 0)
    		return n / 2;
    	else
    		return 3 * n + 1;
    }
    inline long long cycleLength(long long n)
    {
    	if (n == 1)		//base case
    		return 1;
    	// check if we've already cached the
    	// cycle length of the current number
    	if (n < limit && cache[n] != 0)	
    		return cache[n];
    	long long length = 1 + cycleLength(next(n));
    	cache[n] = length;
    	return length;
    }
    int main()
    {
    	int i, j, a, b;
    	long long temp;
    	while (cin >> i >> j)
    	{
    		long long maxLength = 0;
    		if (i <= j)
    			a = i, b = j;
    		else
    			a = j, b = i;
    		for (int itr = a; itr <= b; ++itr)
    		{
    			temp = cycleLength(itr);
    			if (maxLength < temp)
    				maxLength = temp;
    		}
    		cout << i << ' ' << j << ' ' << maxLength << endl;
    	}
    }

  2. #2
    Guest
    Guest
    What does the compiler say? Add a space after your include statements, for starters. Also, is the array to big for the stack maybe?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also, is the array to big for the stack maybe?
    Since the array is in the global scope it is not created on the stack.
    I can't get this code to compile in Microsoft Visual Studio 2013
    Then please post the compile warning/error messages.

    Jim

  4. #4
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    when i commented line 21:longlonglength = 1 + cycleLength(next(n));
    all the obscure messages about include/xutility disappeared.

    Here are the compile errors:
    Code:
    1>------ Build started: Project: 3n+1(with recursive), Configuration: Debug Win32 ------
    1>  3n+1(with recursive).cpp
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C2825: '_Iter': must be a class or namespace when followed by '::'
    1>          c:\users\vishal\documents\visual studio 2013\projects\challenges\3n+1(with recursive)\3n+1(with recursive)\3n+1(with recursive).cpp(21) : see reference to class template instantiation 'std::iterator_traits<__int64>' being compiled
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C2039: 'iterator_category' : is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C2146: syntax error : missing ';' before identifier 'iterator_category'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C2602: 'std::iterator_traits<__int64>::iterator_category' is not a member of a base class of 'std::iterator_traits<__int64>'
    1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371) : see declaration of 'std::iterator_traits<__int64>::iterator_category'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C2868: 'std::iterator_traits<__int64>::iterator_category' : illegal syntax for using-declaration; expected qualified-name
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(372): error C2825: '_Iter': must be a class or namespace when followed by '::'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(372): error C2039: 'value_type' : is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(372): error C2146: syntax error : missing ';' before identifier 'value_type'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(372): error C2602: 'std::iterator_traits<__int64>::value_type' is not a member of a base class of 'std::iterator_traits<__int64>'
    1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(372) : see declaration of 'std::iterator_traits<__int64>::value_type'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(372): error C2868: 'std::iterator_traits<__int64>::value_type' : illegal syntax for using-declaration; expected qualified-name
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(373): error C2825: '_Iter': must be a class or namespace when followed by '::'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(373): error C2039: 'difference_type' : is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(373): error C2146: syntax error : missing ';' before identifier 'difference_type'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(373): error C2602: 'std::iterator_traits<__int64>::difference_type' is not a member of a base class of 'std::iterator_traits<__int64>'
    1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(373) : see declaration of 'std::iterator_traits<__int64>::difference_type'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(373): error C2868: 'std::iterator_traits<__int64>::difference_type' : illegal syntax for using-declaration; expected qualified-name
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(375): error C2825: '_Iter': must be a class or namespace when followed by '::'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(375): error C2039: 'pointer' : is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(375): error C2146: syntax error : missing ';' before identifier 'pointer'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(375): error C2602: 'std::iterator_traits<__int64>::pointer' is not a member of a base class of 'std::iterator_traits<__int64>'
    1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(375) : see declaration of 'std::iterator_traits<__int64>::pointer'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(375): error C2868: 'std::iterator_traits<__int64>::pointer' : illegal syntax for using-declaration; expected qualified-name
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(376): error C2825: '_Iter': must be a class or namespace when followed by '::'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(376): error C2039: 'reference' : is not a member of '`global namespace''
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(376): error C2146: syntax error : missing ';' before identifier 'reference'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(376): error C2602: 'std::iterator_traits<__int64>::reference' is not a member of a base class of 'std::iterator_traits<__int64>'
    1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(376) : see declaration of 'std::iterator_traits<__int64>::reference'
    1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(376): error C2868: 'std::iterator_traits<__int64>::reference' : illegal syntax for using-declaration; expected qualified-name
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  5. #5
    Guest
    Guest
    Clash with std::next? Try renaming your next function.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    I fixed it. the problem was using namespace std. move it INTO main to fix. There is an ambiguity error with xutility in visual studio.

    P.s the solution was the first google hit when I entered "xutility _ITer must be namespace".

    P.p.s of course a better solution is to just rename next that way you can use your function in main() too if you need.
    Last edited by jiggunjer; 07-11-2015 at 03:11 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The solution is to stop using "using namespace std".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The solution is to stop using "using namespace std".
    O_o

    No. The solution is to correctly use the namespace mechanism.

    You can be specific in the function call.

    You could use a directive in a more local scope.

    You can do a lot, but you shouldn't avoid using directives.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I like to avoid importing things into the global scope at all times. Can cause conflicts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think you're both right (Elysia and phantom).

    I hate
    Code:
    using namespace std;
    I don't care that
    Code:
    std::vector<int> x;
    is uglier than
    Code:
    vector<int> x
    . I think it's much more specific and up-front what's going on with the first vector declaration.

    But at the same time, phantom's right, using namespaces properly is the ideal.

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I like to avoid importing things into the global scope at all times. Can cause conflicts.
    O_o

    The scope of the directive, or declaration if you prefer the approach, isn't causing the problem.

    A shared identifier is being found which is either a better fit for the arguments used or introduces an ambiguity because no possibility represents a better fit.

    The directive, the `using namespace std;` statement, within the `cycleLength` function has the same problem.

    The declaration, a `using std::next;` statement, within the `cycleLength` function also has the same problem.

    As I said, the solution isn't avoiding `using` statements.

    If you avoid `using` statements, you'll always need to fully specify names. The approach is exactly as bad as needing to prefix library names as in the C language. Of course, you also lose Argument-dependent Name Lookup by avoiding the `using` statements.

    The solution is actually learning to make the namespace mechanism work for you.

    The important part of the real solution is knowing that `cycleLength` depends on a particular function and not simple any function with a compatible interface with respect to the arguments used. The `cycleLength` function specifically wants the globally declared/defined `next` function. The namespace mechanism allows you, as the developer, to accomplish exactly such specificity when and where appropriate.

    You can introduce the same problem, as shown in the original post, with some simple code. I'll even go a step further by showing that the scope of the `using` statement isn't relevant when you have a problem if you don't know how to actually solve the problem.

    [Edit]
    I should note that putting a `using` statement at global scope in a header is a terrible practice.

    A `using` statement at global scope in a header imports the name in every translation unit which includes the header.

    You should limit `using` directives to a smaller scope when appearing in a header.
    [/Edit]

    Soma

    Code:
    #include <iostream>
    #include <string>
    namespace Scope1
    {
        template
        <
            typename F
        >
        void Test
        (
            F f
        )
        {
            typename F::R s;
        }
    }
    void Test
    (
        unsigned int f
    )
    {
        std::cout << "::Test" << '\n';
    }
    class Variant1
    {
        Variant1()
        {
            using namespace Scope1;
            //Test(1); // Fail.
            ::Test(1); // Fine.
        }
    };
    class Variant2
    {
        Variant2()
        {
            using Scope1::Test;
            //Test(2); // Fail.
            ::Test(2); // Fine.
        }
    };
    namespace Scope2
    {
        using namespace Scope1;
        class Variant3
        {
            Variant3()
            {
                //Test(3); // Fail.
                ::Test(3); // Fine.
            }
        };
    }
    namespace Scope2
    {
        using Scope1::Test;
        class Variant4
        {
            Variant4()
            {
                //Test(4); // Fail.
                ::Test(4); // Fine.
            }
        };
    }
    int main()
    {
        return(0);
    }
    Last edited by phantomotap; 07-11-2015 at 06:42 PM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am getting an error at compile time.
    By Justinweq in forum C Programming
    Replies: 1
    Last Post: 11-19-2013, 06:29 PM
  2. compile time error
    By kapil1089thekin in forum C++ Programming
    Replies: 4
    Last Post: 09-01-2010, 12:29 PM
  3. compile time error or runtime error?
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-07-2008, 07:08 AM
  4. Compile time error
    By j_spinto in forum C Programming
    Replies: 31
    Last Post: 05-06-2005, 10:41 AM
  5. compile time error...
    By Ruchikar in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2002, 02:51 AM