zoukankan      html  css  js  c++  java
  • vc10的C2664和C2065错误

        在vs2010中编译一个普通的C++程序(Win32 Console Application),都会出现这两个错误!

        究其原因是:我们已经习惯了VC6的种种简陋和不规范!

        例如,下列程序在VC6中编译通过。

        主程序:testCir2.cpp

    // testCir2.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include "circular.h"
    #include <stdlib.h>
    #include <iostream.h>
    
    int main(int argc, char* argv[])
    {
    	const double Pi = 3.14;
    	double dRadius = 3;
    	if (argc > 1) {
    		dRadius = atof(argv[1]);
    	}
    
    	cout<<"你输入的半径为: "<<dRadius<<endl;
    
    	Circular *circular = new Circular(Pi);
    
    	double dArea = circular->getArea(dRadius);
    	cout<<"面积为:"<<dArea<<endl;
    
    	double dCircumference = circular->getCircumference(dRadius);
    	cout<<"周长为:"<<dCircumference<<endl;
    
    	return 0;
    }

       

        但是在vc10中就会出现:

        1. C2664: 'atof' : cannot convert parameter 1 from '_TCHAR *' to 'const char *'

    dRadius = atof(argv[1]); // vc6 // C2664: 'atof' : cannot convert parameter 1 from '_TCHAR *' to 'const char *'

        原因是:VC10中使用了unicode定义的变量;我们的MBCS定义的函数无法进行转换工作。

    TCHAR.H routine

    _UNICODE & _MBCS not defined

    _MBCS defined

    _UNICODE defined

    _tstof

    atof

    atof

    _wtof

    _ttof

    atof

    atof

    _wtof

        更改为:

    dRadius = _wtof(argv[1]);

        即可解决C2664错误。

        

        2. C2065: 'cout' : undeclared identifier

            C2065: 'endl' : undeclared identifier

            我们经常使用的cout和endl怎么变成了不识别的了?

            原因是:VC10给标准函数使用了命名空间。

            解决方法有2种
            (1) 强制使用命名空间

    using namespace std;

            (2) 在标准函数前加前缀

    std::cout<<"你输入的半径为: "<<dRadius<<std::endl;


        最后,要注意引用的不同:

        VC6:

    #include <stdlib.h> // vc6 - atof()
    #include <iostream.h> // vc6 - cout // vc6 - endl

       VC10:

    // vc10 - cout & endl
    using namespace std;
    #include <iostream>




    --------------------------------------------------------------xiaobin_hlj80--------------------------

    附:类文件

         头文件:circular.h

    // circular.h: interface for the Circular class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_CIRCULAR_H__612399AD_E8A7_433A_BD63_6C1F29BAC83E__INCLUDED_)
    #define AFX_CIRCULAR_H__612399AD_E8A7_433A_BD63_6C1F29BAC83E__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    class Circular  
    {
    public:
    	Circular(double pi);
    	virtual ~Circular();
    
    	double PI;
    
    	double getArea(double radius);
    
    	double getCircumference(double radius);
    
    };
    
    #endif // !defined(AFX_CIRCULAR_H__612399AD_E8A7_433A_BD63_6C1F29BAC83E__INCLUDED_)

        源文件:circular.cpp

    // circular.cpp: implementation of the Circular class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "stdafx.h"
    #include "circular.h"
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    Circular::Circular(double pi)
    {
    	PI = pi;
    }
    
    Circular::~Circular()
    {
    
    }
    
    double Circular::getArea(double radius) {
    	return PI * (radius * radius);
    }
    
    double Circular::getCircumference(double radius) {
    	return PI * (radius * 2);
    }
    
  • 相关阅读:
    1014 Waiting in Line (30)(30 point(s))
    1013 Battle Over Cities (25)(25 point(s))
    1012 The Best Rank (25)(25 point(s))
    1011 World Cup Betting (20)(20 point(s))
    1010 Radix (25)(25 point(s))
    1009 Product of Polynomials (25)(25 point(s))
    1008 Elevator (20)(20 point(s))
    1007 Maximum Subsequence Sum (25)(25 point(s))
    1006 Sign In and Sign Out (25)(25 point(s))
    1005 Spell It Right (20)(20 point(s))
  • 原文地址:https://www.cnblogs.com/pangblog/p/3339546.html
Copyright © 2011-2022 走看看