Thread: is it possible to implement it in this way

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    is it possible to implement it in this way

    this is just a program that can no be compiled successfully. thus, could anyone provides a little advice for this. thanks
    Code:
    // MMath.h : interface of the MMath class
    
    #pragma once
    
    #ifndef  __MMATH_H__
    #define  __MMATH_H__
    
    #include <iostream>
    #include <iomanip>
    #include <vector>
    
    #endif
    
    template <typename T, size_t R, size_t C>
    class   YMatrix
    {
    	// Constructor & Destructor
    public:
    	YMatrix();
    	~YMatrix();
    //	YMatrix(std::vector< std::vector<T> > & valv);  // a overload constructor
    	YMatrix(T (&arr));
    
    	// Operations
    public:
    
    	//for test
    public:
    //	YMatrix(T val);
    
    //	T & operator=(T ori);
    	void yout();
    //	void yset(T val);
    
    	// members
    private:
    //	std::vector< std::vector<T> > mem;   // 2 dims vector for storing matrix data
    	T test;                              // a private member variable for test program running
    	T mem[R][C];
    };
    Code:
    #include "mat.h"
    
    template<typename T, size_t R, size_t C> YMatrix<T,R,C>::YMatrix() 
    {
    
    };
    
    template<typename T, size_t R, size_t C> YMatrix<T,R,C>::~YMatrix() 
    {
    
    };
    
    template<typename T, size_t R, size_t C> void YMatrix<T,R,C>::yout()
    {
    	for(int i=0; i<R; i++)
    	{
    		for(int j=0; j<C; j++)
    			std::cout<<mem[i][j];
    		std::cout<<std::endl;
    	}
    };
    Code:
    #include "mat.cpp"
    
    void test()
    {
    	float ar[2][2] = {{1,2},{3.5,4.0}};
    	YMatrix<float, 2, 2> Ma(ar);
    	Ma.yout();
    }
    int main(void)
    {
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    When dealing with templates, both the definition and the implementation must be in the same compilation unit (usually the same file).

    Jim

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In other words, get rid of mat.cpp and put its contents back into mat.h

    You should also not include a destructor when it has no work to do and is non-virtual.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Code:
    #ifndef  __MMATH_H__
    #define  __MMATH_H__
    Header names beginning with underscores are reserved.

    Quote Originally Posted by 1999 C Standard
    7.1.3 Reserved identifiers

    Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

    All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
    All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
    Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).
    All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.154
    Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.
    No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

    If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.

    154) The list of reserved identifiers with external linkage includes errno, math_errhandling, setjmp, and va_end.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    o_O
    I think it would be appropriate to citate the C++ standard while in the C++ forum.
    Although with this, it doesn't matter, since the same thing applies to C++ too.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to implement a big int class in c++?
    By makonikor in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2011, 10:55 AM
  2. how to implement in C++
    By nageshrk1 in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2010, 01:16 PM
  3. Replies: 9
    Last Post: 11-12-2007, 03:29 PM
  4. What should I Implement next?
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2006, 11:37 AM
  5. Can't Implement Tabbing
    By Invincible in forum Windows Programming
    Replies: 10
    Last Post: 02-27-2002, 05:09 AM